-->
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.  [ 11 posts ] 
Author Message
 Post subject: Null Pointer Exception
PostPosted: Fri Apr 13, 2007 12:18 pm 
Newbie

Joined: Fri Apr 13, 2007 12:12 pm
Posts: 7
Hello all,

I am trying to run a very basic application which inserts one record into a table created in a SQL Server database.

Here is my hibernate.cfg.xml file:

<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:microsoft:sqlserver://my_server_name/my_database_name</property>
<property name="hibernate.connection.username">testuser</property>
<property name="hibernate.connection.password">testpass</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>

Here is my Java class:

public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.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("test first");
contact.setLastName("last name");
contact.setEmail("xyz@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();

}

And the error occurs at "session.flush()".

Please help me out ASAP.

Thanks. BK.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 12:26 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
your session isn't being instantiated in the try block, and cause it's in the finally block you're trying to close a null session.



find out what the real error is.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 12:38 pm 
Newbie

Joined: Fri Apr 13, 2007 12:12 pm
Posts: 7
thx kochp.

everything is being done in the TRY block, even the instantiation.
should i move "session.flush()" to TRY block? I tried that too. but didn't work.

this is the error i am getting:
Exception in thread "main" java.lang.NullPointerException

and this is my contact.hbm.xml file:

<class name="mypackage.Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>

please look into it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 1:05 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
no. find out why your session isn't being instantiated. I'm guessing your real problem is comming from one of these lines throwing an exception.

Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 1:26 pm 
Newbie

Joined: Fri Apr 13, 2007 12:12 pm
Posts: 7
thx. i created try&catch blocks for each statement and tested. I did not find any problem with creating a session.
i modified my class a bit: look at it:

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch(Exception e1){
System.out.println("\nsession instantiation failed\n");
}
try {
session =sessionFactory.openSession();
}
catch(Exception e1){
System.out.println("\nsession creation failed\n");
}
try {
//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(1);
contact.setFirstName("test");
contact.setLastName("test");
contact.setEmail("test@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
try {
session.flush();
}
catch(Exception e){
System.out.println("\n\nActual Save failed");
session.close();
}

}

In my Error console, "Actual Save failed" was printed out. so, there is an error while saving and all db properties are fine. can you think of any solution?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 1:46 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
yeah. look at your logs and find out the cause.

save failed could mean a billion things.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:20 pm 
Newbie

Joined: Fri Apr 13, 2007 12:12 pm
Posts: 7
Now, this is the error I am getting. I really don't understand what's goin on. I gave all correct DB properties but still.............:(

org.hibernate.exception.GenericJDBCException: Cannot open connection
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Unable to connect. Invalid URL.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)



please lemme know dude.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:22 pm 
Newbie

Joined: Fri Apr 13, 2007 12:12 pm
Posts: 7
i am sorry.

i gave an incorrect URL earlier. This is the error I am getting now:

Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:33 pm 
Newbie

Joined: Fri Apr 13, 2007 12:12 pm
Posts: 7
hey kochcp,

i think i found the problem. but, tell me where do you specify the name of the DATABASE in hibernate.cfg.xml file?

this is my entry for db URL:

<property name="hibernate.connection.url">jdbc:microsoft:sqlserver://my_database_server_name:1433</property>

but, if I include database name with the server name like: my_database_server_name/database_name, I am getting an error.

lemme know ur thoughts


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 4:03 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
It's amazing how much easier it is to find out what a problems is when you look in the log files.

tack it on to the end of the url after the port

Code:

<property name="hibernate.connection.url">jdbc:jtds:sqlserver://fakedb.fakecompany.com:1433/accountsdb</property>



oh, and if you can, use the jtds drivers, they don't suck unlike the M$ ones

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 2:26 pm 
Newbie

Joined: Fri Apr 13, 2007 12:12 pm
Posts: 7
Thanks once again for the reply dude. I used JTDS drivers now, but for some reason, the record is not getting saved in the database. Here are my logs:

0 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.0rc1
0 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
0 [main] INFO org.hibernate.cfg.Environment - using CGLIB reflection optimizer
0 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
0 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
0 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
266 [main] INFO org.hibernate.cfg.Configuration - Mapping resource: contact.hbm.xml
641 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: roseindia.tutorial.hibernate.Contact -> CONTACT
688 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
688 [main] INFO org.hibernate.cfg.Configuration - processing extends queue
688 [main] INFO org.hibernate.cfg.Configuration - processing collection mappings
688 [main] INFO org.hibernate.cfg.Configuration - processing association property references
688 [main] INFO org.hibernate.cfg.Configuration - processing foreign key constraints
766 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.SQLServerDialect
782 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
782 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
782 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
782 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
782 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
782 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
797 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
922 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 10
922 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
938 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://<db_server>:1433/<database>
938 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=****, password=****}
1532 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
1532 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
1532 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
1532 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
1532 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
1532 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
1532 [main] INFO org.hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.EhCacheProvider
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: enabled
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
1547 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
1750 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
1782 [main] WARN net.sf.ehcache.config.Configurator - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/BK/test%20projects/hibernate/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
2219 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
2219 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.SQLServerDialect
2219 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
2219 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 10
2219 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
2219 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://<db_server>:1433/<database>
2219 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=****, password=****}
2219 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - Running hbm2ddl schema update
2219 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - fetching database metadata
2391 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - updating schema
2391 [main] INFO org.hibernate.cfg.Configuration - processing extends queue
2391 [main] INFO org.hibernate.cfg.Configuration - processing collection mappings
2391 [main] INFO org.hibernate.cfg.Configuration - processing association property references
2391 [main] INFO org.hibernate.cfg.Configuration - processing foreign key constraints
2532 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - table found: <db>.<user>.<table>
2532 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - columns: [lastname, firstname, email, id]
2532 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - foreign keys: []
2532 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - indexes: [pk__contact__759922ab]
2532 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete
2563 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:jtds:sqlserver://<db_server>:1433/<database>
2563 [main] INFO org.hibernate.impl.SessionFactoryImpl - Checking 0 named queries
2579 [Finalizer] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:jtds:sqlserver://<db_server>:1433/<database>
Inserting Record
Done
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)


Any thoughts?


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