Results 1 to 16 of 16
  1. #1
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default 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

  2. #2
    kitty4uhihg is offline Member
    Join Date
    Jul 2008
    Posts
    1
    Rep Power
    0

    Angry hi

    I don't want to restart PlayPod that often. Ten minutes is not enough to evaluate the product.

  3. #3
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    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?

  5. #5
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default

    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)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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,

    Java Code:
    for( int j = 0; j < inventory.length - 1; j++ )
    what happened after j exceed 4 is, all values set to null. So you found a null and comes with null pointer exception.

  7. #7
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default

    Eranga,

    I cannot thank you enough. I changed my array and now my program works. Thanks again.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    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() {
    }

    }

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default 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)

  12. #12
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    439
    Rep Power
    6

    Default

    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)

  13. #13
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    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>

  14. #14
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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
    Hibernate not finding its config file could be a problem...

    Learn to skim over the error messages, looking for one that actually means something. Usually, it is near the top.

  15. #15
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default 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

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2008, 01:37 AM
  2. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  3. Replies: 1
    Last Post: 08-06-2007, 06:07 AM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •