-->
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.  [ 7 posts ] 
Author Message
 Post subject: Another NullPointerException error - on getter method
PostPosted: Fri May 04, 2007 4:00 pm 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
Need a bit of help here... I have a simple database that I am mapping one table, USERS, to a class, Users. In my Test class, I am simply trying to print a list of each user's name. I can get the count of all users, however whenever I try the following line:
Code:
System.out.println ( user.getName() );
it throws a NullPointerException error.

Here is my code for the Test class:
Code:
public class Test{
      public static void main (String[] args)
    {
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      Session session = sessionFactory.openSession();

      session.beginTransaction();
      
         Criteria crit = session.createCriteria(Users.class);
         List result = crit.list();
         
         System.out.println("FOUND: "+result.size());
         for (Iterator iter = result.iterator(); iter.hasNext();){
            Users user = (Users) iter.next();   //}
            System.out.println ("USERNAME: "+user.getName());      }
      
      session.getTransaction().commit();
      session.close();
   }
}


And here is the contents of my hibernate log file:
Code:
15:32:33,315  INFO Environment:500 - Hibernate 3.2.0.cr5
15:32:33,325  INFO Environment:533 - hibernate.properties not found
15:32:33,325  INFO Environment:667 - Bytecode provider name : cglib
15:32:33,325  INFO Environment:584 - using JDK 1.4 java.sql.Timestamp handling
15:32:33,425  INFO Configuration:1350 - configuring from resource: /hibernate.cfg.xml
15:32:33,425  INFO Configuration:1327 - Configuration resource: /hibernate.cfg.xml
15:32:33,525  INFO Configuration:507 - Reading mappings from resource: com\pac\server\common\oraclevars\OracleVars.hbm.xml
15:32:33,625  INFO HbmBinder:300 - Mapping class: com.pac.server.common.oraclevars.Workstations -> WORKSTATIONS
15:32:33,665  INFO HbmBinder:300 - Mapping class: com.pac.server.common.oraclevars.Users -> USERS
15:32:33,665  INFO Configuration:1465 - Configured SessionFactory: null
15:32:33,745  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
15:32:33,745  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 10
15:32:33,745  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
15:32:33,836  INFO DriverManagerConnectionProvider:80 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@localhost:1521:localdb
15:32:33,836  INFO DriverManagerConnectionProvider:86 - connection properties: {user=mpac_mgr, password=****}
15:32:34,316  INFO SettingsFactory:81 - RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
15:32:34,326  INFO SettingsFactory:82 - JDBC driver: Oracle JDBC driver, version: 9.2.0.1.0
15:32:34,346  INFO Dialect:141 - Using dialect: org.hibernate.dialect.OracleDialect
15:32:34,356  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
15:32:34,356  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
15:32:34,356  INFO SettingsFactory:134 - Automatic flush during beforeCompletion(): disabled
15:32:34,356  INFO SettingsFactory:138 - Automatic session close at end of transaction: disabled
15:32:34,356  INFO SettingsFactory:145 - JDBC batch size: 15
15:32:34,356  INFO SettingsFactory:148 - JDBC batch updates for versioned data: disabled
15:32:34,366  INFO SettingsFactory:153 - Scrollable result sets: enabled
15:32:34,366  INFO SettingsFactory:161 - JDBC3 getGeneratedKeys(): disabled
15:32:34,366  INFO SettingsFactory:169 - Connection release mode: auto
15:32:34,366  INFO SettingsFactory:196 - Default batch fetch size: 1
15:32:34,366  INFO SettingsFactory:200 - Generate SQL with comments: disabled
15:32:34,366  INFO SettingsFactory:204 - Order SQL updates by primary key: disabled
15:32:34,366  INFO SettingsFactory:369 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
15:32:34,366  INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
15:32:34,366  INFO SettingsFactory:212 - Query language substitutions: {}
15:32:34,366  INFO SettingsFactory:217 - JPA-QL strict compliance: disabled
15:32:34,366  INFO SettingsFactory:222 - Second-level cache: enabled
15:32:34,366  INFO SettingsFactory:226 - Query cache: disabled
15:32:34,366  INFO SettingsFactory:356 - Cache provider: org.hibernate.cache.NoCacheProvider
15:32:34,366  INFO SettingsFactory:241 - Optimize cache for minimal puts: disabled
15:32:34,366  INFO SettingsFactory:250 - Structured second-level cache entries: disabled
15:32:34,376  INFO SettingsFactory:270 - Echoing all SQL to stdout
15:32:34,376  INFO SettingsFactory:277 - Statistics: disabled
15:32:34,376  INFO SettingsFactory:281 - Deleted entity synthetic identifier rollback: disabled
15:32:34,376  INFO SettingsFactory:296 - Default entity-mode: pojo
15:32:34,447  INFO SessionFactoryImpl:161 - building session factory
15:32:34,777  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
15:32:34,777  INFO SchemaUpdate:115 - Running hbm2ddl schema update
15:32:34,777  INFO SchemaUpdate:126 - fetching database metadata
15:32:34,787  INFO SchemaUpdate:138 - updating schema
15:32:34,927  INFO TableMetadata:40 - table found: MPAC_MGR.USERS
15:32:34,927  INFO TableMetadata:41 - columns: [password, actions, name, id]
15:32:34,927  INFO TableMetadata:43 - foreign keys: []
15:32:34,927  INFO TableMetadata:44 - indexes: []
15:32:35,017  INFO TableMetadata:40 - table found: MPAC_MGR.WORKSTATIONS
15:32:35,027  INFO TableMetadata:41 - columns: [actions, id]
15:32:35,027  INFO TableMetadata:43 - foreign keys: []
15:32:35,027  INFO TableMetadata:44 - indexes: []
15:32:35,027  INFO SchemaUpdate:160 - schema update complete
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_
15:32:35,148 DEBUG IntegerType:122 - returning '5' as column: ID1_0_
15:32:35,158 DEBUG StringType:122 - returning 'Freddie' as column: NAME1_0_
15:32:35,158 DEBUG StringType:122 - returning 'PICK,PACK' as column: ACTIONS1_0_
15:32:35,158 DEBUG StringType:122 - returning 'Flintstone' as column: PASSWORD1_0_
15:32:35,168 DEBUG IntegerType:122 - returning '5' as column: ID1_0_

(repeat the last line many times over - the number of times corresponds to the number of records in the database)

So my question is, what am I doing wrong?
I tracked down as many different examples as I could, but I am still not grasping what the problem is. (BTW - I'm new to Hibernate)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 4:05 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Can we see your mapping file for Users?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 4:09 pm 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
Absolutely. And further below is the console output if needed:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="com.pac.server.common.oraclevars.Users" table="USERS">
      <id name="id" type="int" column="ID" >
         <generator class="assigned"/>
      </id>

      <property name="Name">
         <column name="NAME" />
      </property>
      <property name="Actions">
         <column name="ACTIONS"/>
      </property>
      <property name="Password">
         <column name="PASSWORD"/>
      </property>
   </class>

</hibernate-mapping>







Code:
     [java] Hibernate: select this_.ID as ID1_0_, this_.NAME as NAME1_0_, this_.
ACTIONS as ACTIONS1_0_, this_.PASSWORD as PASSWORD1_0_ from USERS this_
     [java] FOUND: 55
     [java] java.lang.NullPointerException
     [java]     at com.pac.server.common.mpaccp07.Mpaccp07.main(Unknown Source)
     [java] Exception in thread "main"
     [java] Java Result: 1
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 4:17 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Maybe it's late in the day, but I just don't see the problem. Firstly, try a production release of Hibernate, looks like you are still using CR5 of 3.2. Gotta leave the office soon, but I'll check in when I get home.

This looks ominous though,
Code:
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_
15:32:35,148 DEBUG IntegerType:116 - returning null as column: ID1_0_


Null safe the user before the system.out.

Check the database rows to see if there's anything goofy in there (all nulls in a row, etc).


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 4:45 pm 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
Looks like you were right... found a record in the database with several Null fields, one of which is the ID field. Replaced that with a valid value, and all seems well now. Guess that stresses the importance of defining a primary key in a database...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 9:09 pm 
Newbie

Joined: Fri May 04, 2007 3:43 pm
Posts: 18
Ananasi wrote:
Null safe the user before the system.out.


Does that mean test user for null? (Do you have an example of the best practice to do this?). Sorry, new terminology to me all around.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 8:05 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Yeah brother, it just means to do if (user != null) { do stuff here with user} . Always good practice to make sure objects are not null before invoking their methods if there is the slightest possibility it could be null. I hate NPE's ! ;D


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