Results 1 to 16 of 16
- 08-05-2008, 12:06 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 43
- Rep Power
- 0
Exception in thread "main" java.lang,NullPointerException
Please help!
I am getting the following error in the program I have provided below. "Exception in thread "main" java.lang.NullPointerException." Can anyone help me? Thank you.
Java Code:// CheckPoint: Inventory4.java // Week 7 // This program calculates inventory value public class Inventory4 { public static void main( String args[] ) { double restockingFee = 0.05; manufacturer[] inventory = new manufacturer[100]; inventory[0] = new manufacturer ( "notepads", restockingFee, "4000", "Ampad", 60, 2.75 ); inventory[1] = new manufacturer ( "pencils", restockingFee, "5000", "Bic", 75, 1.25 ); inventory[2] = new manufacturer ( "folders", restockingFee, "2000", "3M", 30, 4.75 ); inventory[3] = new manufacturer ( "envelopes", restockingFee, "1000", "Universal", 15, 5.25 ); inventory[4] = new manufacturer ( "markers", restockingFee, "3000", "Sanford", 45, 3.50 ); manufacturer temp[] = new manufacturer[1]; for( int j = 0; j < inventory.length - 1; j++ ) { for( int k = 0; k < inventory.length - 1; k++ ) { if( inventory[k].getItemName().compareToIgnoreCase ( inventory[k+1].getItemName() ) > 0 ) { temp[0] = inventory[k]; inventory[k] = inventory[k+1]; inventory[k+1] = temp[0]; } } } javax.swing.JTextArea ta = new javax.swing.JTextArea( 10, 20 ); for( int j = 0; j < inventory.length; j++ ) { // System.out.println( inventory[j].toString() );<----------------- ta.append( inventory[j].toString()+"\n" ); } // System.out.printf( "Total value of all inventory = $%.2f", // Inventory.etTotalValueOfAllInventory( inventory ) ); // return; // <------------------above line and this line ta.append("Total value of all inventory = "+product.getTotalValueOfAllInventory(inventory)); javax.swing.JFrame frame = new javax.swing.JFrame(); frame.getContentPane().add( new javax.swing.JScrollPane(ta) ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE ); frame.setVisible( true ); } } // The product class class product { public String productNumber; public String name; public int numberOfUnits; public double pricePerUnit; // create a new instance of Inventory // main constructor for the class product( String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price ) { productNumber = Item_Number; name = Item_Name; numberOfUnits = Items_in_Stock; pricePerUnit = Item_Price; } // sets the product name public void setItemName( String Item_Name ) { name = Item_Name; } // sets the product number public void setItemNumber( String Item_Number ) { productNumber = Item_Number; } // sets the number of units in stock public void setItemsInStock( int Items_in_Stock ) { numberOfUnits = Items_in_Stock; } // sets the price of the product public void setItemPrice( double Item_Price ) { pricePerUnit = Item_Price; } // returns product name public String getItemName() { return name; } // returns product number public String getItemNumber() { return productNumber; } // returns units in stock public int getItemsInStock() { return numberOfUnits; } // returns product price public double getItemPrice() { return pricePerUnit; } // returns the total value of inventory public double getInventoryValue() { return pricePerUnit * numberOfUnits; } public static double getTotalValueOfAllInventory( product[] inv ) { double tot = 0.0; for( int i = 0; i < inv.length; i++ ) { tot += inv[i].getInventoryValue(); } return tot; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append( "Product Name: \t" ).append( name ).append( "\n" ); sb.append( "Product Number: \t" ).append( productNumber ).append( "\n" ); sb.append( "Units in Stock: \t" ).append( numberOfUnits ).append( "\n" ); sb.append( "Unit Price: \t" ).append( String.format( "$%.2f%n", pricePerUnit ) ); sb.append( "Inventory Value: \t" ).append(String.format("$%.2f%n", this.getInventoryValue())); return sb.toString(); } } class manufacturer extends product { // manufacturer of product private String manufacturer; // percentage added to base price as restocking fee private double restockingFee; // actual inventory value will be the base inventory value plus // the base inventory value times this amount ( value + ( value * restockingFee ) ) public manufacturer( String manufacturer, double restockingFee, String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price) { super( Item_Number, Item_Name, Items_in_Stock, Item_Price); this.manufacturer = manufacturer; this.restockingFee = restockingFee; } // returns inventory value plus the restocking fee public double getInventoryValue() { return super.getInventoryValue() + ( super.getInventoryValue() * restockingFee ); } // end getInventoryValue public String toString() { StringBuffer sb = new StringBuffer( "Manufacturer: \t" ).append( manufacturer ).append( "\n" ); sb.append( super.toString() ); return sb.toString(); } // end toString } // end class manufactuer
- 08-05-2008, 01:15 AM #2
Member
- Join Date
- Jul 2008
- Posts
- 1
- Rep Power
- 0
hi
I don't want to restart PlayPod that often. Ten minutes is not enough to evaluate the product.
- 08-05-2008, 01:27 AM #3
Member
- Join Date
- Jul 2008
- Posts
- 43
- Rep Power
- 0
Can someone else answer my question. I do not understand this member's response. This is an assignment that has already been graded. I just need to get this error fixed so I can move on to Part 5 of the project. I need an answer as soon as possible since it seems to take me almost a week to do my assignments in this class.
- 08-05-2008, 02:27 AM #4
When you get an error message, you need to post ALL of the text of the message. The message includes the line number where the error occured. Look at the error message, find the line number and examine that line in your program. What object in that line could be null?
- 08-05-2008, 02:57 AM #5
Member
- Join Date
- Jul 2008
- Posts
- 43
- Rep Power
- 0
Norm,
Sorry about that. Here is the whole error message. I reviewed line 29 and cannot see what is wrong with it.
Exception in thread "main" java.lang.NullPointerException
at Inventory4.main(Inventory4.java:29)
- 08-05-2008, 03:57 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What happened there is you define an array of type manufacture of size 100. But you have only initialized 5 of them. Once you loop through the array length as follows,
what happened after j exceed 4 is, all values set to null. So you found a null and comes with null pointer exception.Java Code:for( int j = 0; j < inventory.length - 1; j++ )
- 08-05-2008, 05:20 AM #7
Member
- Join Date
- Jul 2008
- Posts
- 43
- Rep Power
- 0
Eranga,
I cannot thank you enough. I changed my array and now my program works. Thanks again.
- 08-05-2008, 05:24 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's pleasure to help you. If you solve the question, you can mark the thread as solved.
- 02-09-2009, 11:30 AM #9
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
hi every one....can any one help me out....whenever i run this program i got Exception in thread "main" java.lang.NullPointerException
it says net.sf.hibernate.cfg.Environment - hibernate.properties not found please help me out.....
code
==============
package de.laliluna.example;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static net.sf.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || ! session.isConnected()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private HibernateSessionFactory() {
}
}
- 02-09-2009, 01:02 PM #10
Complete error msg...
Please post the complete error msg... what is important is the line number in the error msg.
Also, you are posting on an old thead ... it wouls be better if you opened a new one for your problem.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-09-2009, 01:22 PM #11
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException
hi thanks for ur reply....here r the error which i m getting.....when i run the program ....
ERROR:->
0 [main] INFO TestClient - creating honey
47 [main] INFO net.sf.hibernate.cfg.Environment - Hibernate 2.1.8
47 [main] INFO net.sf.hibernate.cfg.Environment - hibernate.properties not found
47 [main] INFO net.sf.hibernate.cfg.Environment - using CGLIB reflection optimizer
47 [main] INFO net.sf.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
47 [main] INFO net.sf.hibernate.cfg.Configuration - configuring from resource: D:/brijesh/FirstHibernateExample/hibernate.cfg.xml
47 [main] INFO net.sf.hibernate.cfg.Configuration - Configuration resource: D:/brijesh/FirstHibernateExample/hibernate.cfg.xml
47 [main] WARN net.sf.hibernate.cfg.Configuration - D:/brijesh/FirstHibernateExample/hibernate.cfg.xml not found
%%%% Error Creating SessionFactory %%%%
net.sf.hibernate.HibernateException: D:/brijesh/FirstHibernateExample/hibernate.cfg.xml not found
at net.sf.hibernate.cfg.Configuration.getConfiguratio nInputStream(Configuration.java:886)
at net.sf.hibernate.cfg.Configuration.configure(Confi guration.java:910)
at de.laliluna.example.HibernateSessionFactory.curren tSession(HibernateSessionFactory.java:48)
at de.laliluna.example.TestClient.createHoney(TestCli ent.java:42)
at de.laliluna.example.TestClient.main(TestClient.jav a:24)
Exception in thread "main" java.lang.NullPointerException
at de.laliluna.example.HibernateSessionFactory.curren tSession(HibernateSessionFactory.java:56)
at de.laliluna.example.TestClient.createHoney(TestCli ent.java:42)
at de.laliluna.example.TestClient.main(TestClient.jav a:24)
- 02-12-2009, 07:55 AM #12
Hello
As so long of your thread,no one could understand what you want to solve
please use simple form to post your thread?Mak
(Living @ Virtual World)
- 02-12-2009, 08:15 AM #13
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
hi...i m new to hibernate....i m just working on this program...i can compile it but when i run i got following error....please help me out....thanks
ERROR:-
-----------------------------------------------------------------
Feb 12, 2009 12:42:10 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(Fir stExample.java:28
CODE:-
------------------------------------------------------------------
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class FirstExample {
public static void main(String[] args) {
Session session = null;
try{
// This step will read hibernate.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}
XML:-
---------------------------------------------------------------
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.micro soft.sqlserver.jdbc.SQLServerDrive</property>
<property name="hibernate.connection.url">jdbc:sqlserver://192.168.0.187:1433</property>
<property name="hibernate.connection.username">vt014</property>
<property name="hibernate.connection.password">brij123</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQL ServerDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 02-12-2009, 05:16 PM #14
Hibernate not finding its config file could be a problem...47 [main] WARN net.sf.hibernate.cfg.Configuration - D:/brijesh/FirstHibernateExample/hibernate.cfg.xml not found
%%%% Error Creating SessionFactory %%%%
net.sf.hibernate.HibernateException: D:/brijesh/FirstHibernateExample/hibernate.cfg.xml not found
Learn to skim over the error messages, looking for one that actually means something. Usually, it is near the top.
- 10-12-2010, 04:55 PM #15
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException at exercise3.London
I'm having the same problem. could someone please tell me what to do.
thanks.
import javax.swing.*; // import the swing library for I/O
class exercise3
{
public static void main (String[] param)
{
LondonUnderground();
System.exit(0);
} // END main
/* ************************************************** *
Show how the service on different lines on the London Underground
*/
public static void LondonUnderground()
{
String ans = JOptionPane.showInputDialog("What line would you like to know about?");
if (ans.equalsIgnoreCase ("Central"))
{
JOptionPane.showMessageDialog(null, "The" + ans + " line will be SUSPENDED");
}
else if (ans.equalsIgnoreCase ("Victoria"))
{
JOptionPane.showMessageDialog(null, "The" + ans + " line will have a GOOD SERVICE");
}
else if (ans.equalsIgnoreCase ("District"))
{
JOptionPane.showMessageDialog(null, "The" + ans + " line will have DELAYS");
}
else
{
JOptionPane.showMessageDialog(null, "The line is not recognised by our system");
}
} // END LondonUnderground
} // END class exercise3
-
Please don't hijack another's thread but ask your question in it's own thread.
Locking.
Similar Threads
-
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-29-2008, 01:37 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
Exception in thread "main" java.lang.NoClassDefFoundError
By baltimore in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:07 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks

Bookmarks