-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: NullPointerException reading MySQL table with Hibernate
PostPosted: Fri Jul 13, 2007 10:47 am 
Newbie

Joined: Thu Jul 12, 2007 8:57 am
Posts: 11
Hello,

I have successfully configured hibernate to write data to a database table, and now I am trying to read from it. I want to get back all of the objects that I originally wrote to a table. I have a class Project with attributes id, name, client and location.
I have saved three of these objects in a MySQL table name PROJECT
My database table "PROJECT" has the columns: id, name, client, location (in that order)
The table I am trying to read has:
ROW1-> id: 1 name: name1 client: client1 location: location1
ROW2-> id: 2 name: name2 client: client2 location: location2
ROW3-> id: 3 name: name3 client: client3 location: location3

I am calling this method to see what happens through each iteration:
Code:
   public void listProjects() throws Exception {
        // Create SessionFactory and Session objects
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();

        //Query using Hibernate Query Language
        String hqlString = "FROM Project as p";
        Query query = session.createQuery(hqlString);
       
           //Iterate through stored objects
           for (Iterator it = query.iterate(); it.hasNext();) {
              Project tempProjObj = (Project) it.next();
              System.out.println("Name: " + tempProjObj.getName());
              System.out.println("Client: " + tempProjObj.getClient());
              System.out.println("Location: " + tempProjObj.getLocation());
           }
        session.close();
   }


It's output is the following:

Name: name1
Client: client1
Location: location1
Name: name2
Client: client2
Location: location2
Name: name3
Client: client3
Location: location3
Exception in thread "main" java.lang.NullPointerException
at beans.Tester.main(Tester.java:11)


As you can see, it managed to read all of the rows and get the appropriate values, but still throws a NullPointerException as if it's going for a fourth row. I'd like to find a way to prevent this. Is there a reason why Iterator it.hasNext() is returning true even if the next row doesn't exist? Are there other methods I can call to test for the existance of further rows?

Thank you all for any suggestions!


Top
 Profile  
 
 Post subject: NullPointer caused by Autoboxing?
PostPosted: Fri Jul 13, 2007 11:28 am 
Newbie

Joined: Fri Apr 07, 2006 11:29 am
Posts: 17
If location is an object or numeric, make sure that there are not any numeric values that could be getting the Nullpointer during java autoboxing.

Also, include the full stack trace in [ code ].

The autoboxing exception happens when you have a numeric column in the database and the column is nullable. So when hibernate tries to stick the null value into a, lets say "int" type in java, java autoboxes the value from "Integer" to "int", but does not know how to convert "null" to "int", resulting in a null pointer exception.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 11:56 am 
Newbie

Joined: Thu Jul 12, 2007 8:57 am
Posts: 11
Thanks,
I understand the problem that you describe, but what is the solution for it? I will need to have null values at times, so there should be a workaround for how hibernate inserts the null values.
Each of name, client, location is a String, which of course can be specified as null. I'm guessing that the problem then is the Integer id?

Where should I get the full stack trace? I can list the full Log4J log produced when executing the method if you wish…

Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 2:01 pm 
Newbie

Joined: Thu Jul 12, 2007 8:57 am
Posts: 11
I realised that the NullPointerException was being thrown from my tester class, and not from the reading method. What I had done was set
Code:
//ProjectManager class contains the listProjects() method
ProjectManager pm = new ProjectManager();
      
Project[] projArray = pm.listProjects();
// Iterating through this projArray array does not work because
// it is outside of the hibernate session


It seems that I can't actually access the object that is made from hibernate after the session is closed. The only way I could go around this was to have a constructor in Project for all of the attributes and make a new Project object using the getters on the object that hibernate returned. This seems very clumsy, so I'm wondering, is there anyway that I can use the object that hibernate returned after the session.close() call?

Code:
   public void listProjects() throws Exception {
        // Create SessionFactory and Session objects
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();

        //Query using Hibernate Query Language
        String hqlString = "FROM Project as p";
        Query query = session.createQuery(hqlString);
       
           //Iterate through stored objects
           for (Iterator it = query.iterate(); it.hasNext();) {
              Project tempProjObj = (Project) it.next();
              // tempProjObj's getters can be accessed here
           }
        session.close();
             // tempProjObj's getters can NOT be accessed here
             // unless I make a new object with the attributes
             // before the session.close()
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.