-->
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.  [ 6 posts ] 
Author Message
 Post subject: NullPointerException when querying in Hibernate + JBX
PostPosted: Wed Aug 11, 2004 11:15 pm 
Newbie

Joined: Sun Aug 08, 2004 2:38 am
Posts: 9
Hibernate version:2.1.3

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:MySQL

Debug level Hibernate log excerpt:

Hi,everyone!
I use JBX + Hibernate to write a query method, and show the result in a JTable. Here is my code:


Code:
String rowData[][] = new String[255][5];
UserInfo users[] = null;

public void query(){
    try {
      Configuration conf = new Configuration().addClass(UserInfo.class);
      sessionFactory = conf.buildSessionFactory();
      s = sessionFactory.openSession();
      Transaction t = s.beginTransaction();
    }
    catch (MappingException ex) {
      ex.printStackTrace();
    }
    catch (HibernateException ex) {
      ex.printStackTrace();
    }
    if (tfName.getText() == "all"){
      try {
        tx = s.beginTransaction();
        list = s.find("from UserInfo as ui");
        tx.commit();
        s.flush();
        s.close();
      }
      catch (HibernateException ex1) {
        System.out.println("excute sql1 query failed.");
        ex1.printStackTrace();
      }
    }else{
      try {
       list = s.find("from UserInfo as ui where ui.name = ?" , tfName.getText() , Hibernate.STRING);
       s.flush();
       s.close();
      }
      catch (HibernateException ex2) {
        System.out.println("excute sql2 query failed.");
        ex2.printStackTrace();
      }
    }
    for(int i=0 ; i<list.size() ; i++){
      try{
        if(list == null){
          System.out.println("list equals NULL!");
        }
        System.out.println(list.size());
        System.out.println(i);
        users[i] = (UserInfo)list.get(i);
      }catch(java.lang.NullPointerException ex){
        System.out.println("equals null!");
      }
      rowData[i][0] = users[i].getId();
      rowData[i][1] = users[i].getName();
      rowData[i][2] = users[i].getModuleNo();
      rowData[i][3] = users[i].getMobileNo();
      rowData[i][4] = users[i].getAccount();
    }
    JTable jTable1 = new JTable(rowData,columnName);
    jTable1.setBounds(new Rectangle(14, 109, 338, 109));
    jTable1.setEnabled(false);
    jTable1.setFont(new java.awt.Font("Dialog", 0, 12));
    panel1.add(jTable1, null);
  }


But when running, it always be a NullpointerException. I know I havn't initalize the rowData[][] and users[], but I don't know how to make in works.

Any ideas?

_________________
THINK!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 12, 2004 3:00 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
you call list.size() before checking if null, so the null check in that try block is pointless.
anyhat you now need to figure out why you get a null list back, I guess that's why you're posting, but to help anymore it would be useful to have the info mentioned in the red box.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 12, 2004 6:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I don't see where you ever initialize the users array to anything other than null...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 3:40 am 
Newbie

Joined: Sun Aug 08, 2004 2:38 am
Posts: 9
I've tested that the list is not null, the query return two class entities. So I think list.size() won't be a null, I know maybe it is due to the initialization of the arrays. But I don't know how to initialize them as I don't know the size. Please help~~~

Thanks a lot!

Regards,

_________________
THINK!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 1:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
something like:
Code:
    users = new UserInfo[list.size()];
    for(int i=0 ; i<list.size() ; i++){
      try{
        ...


Although, honestly, I'm mystified as to the entire purpose of this users array.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 14, 2004 11:45 am 
Newbie

Joined: Sun Aug 08, 2004 2:38 am
Posts: 9
Thanks, steve:) Now I know how to deal with it~~~!

Have a nice day!

_________________
THINK!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.