wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate NullPointerException" Watch "Hibernate NullPointerException" New topic
Author

Hibernate NullPointerException

Roman Merkushen
Greenhorn

Joined: May 25, 2004
Posts: 9
While running

Configuration cfg = new Configuration.configure();

throws NullPointerException. I tried both hibernate.properties and hibernate.cfg.xml files, bot are in class path. Configuration file posted below:

<hibernate-configuration>

<!-- a SessionFactory instance listed as /jndi/name -->
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<session-factory name="java:comp/env/hibernate/SessionFactory">

<!-- properties -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="connection.url">jdbc racle:thin:@localhost:1521 b</property>
<property name="connection.datasource">my/first/datasource</property>
<property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
<property name="batch_size">25</property>
<!-- mapping files -->
<mapping resource="com/hibernate/beans/Product.hbm.xml"/>

</session-factory>

</hibernate-configuration>

Please let me know if you know what might be happening.
Thank you.

Java Code:
cfg.addClass(com.hibernate.beans.Product.class);
SessionFactory sessions = cfg.buildSessionFactory();
return sessions.openSession();
[ May 25, 2004: Message edited by: Roman Merkushen ]
Loren Rosen
Ranch Hand

Joined: Feb 12, 2003
Posts: 156
What's the stack trace look like?
Roman Merkushen
Greenhorn

Joined: May 25, 2004
Posts: 9
java.lang.NullPointerException
at com.hibernate.test.Runner.update(Runner.java:57)
at jsp_servlet.__test._jspService(__test.java:173)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6350)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
Loren Rosen
Ranch Hand

Joined: Feb 12, 2003
Posts: 156
I gather that com.hibernate.test.Runner.update is your code. I was somehow thinking that the exception was coming out of the hibernate code.

I think this line
Configuration cfg = new Configuration.configure();

should be
Configuration cfg = new Configuration().configure();

In fact I don't see how the line you have would compile
Roman Merkushen
Greenhorn

Joined: May 25, 2004
Posts: 9
I apologise for posting it wrong. It is
Configuration cfg = new Configuration().configure();
I tried running it as a standalone program and still getting the following stack trace:
java.lang.NullPointerException

at com.hibernate.test.Runner.update(Runner.java:49)

at com.hibernate.test.Runner.main(Runner.java:19)

The code is below:

package com.hibernate.test;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
import com.hibernate.beans.*;
import java.sql.SQLException;

public class Runner
{
public Runner()
{
}

public static void main(String[] args)
{
Runner run = new Runner();
run.update("ULT5005D","ULT5000");

}

/**
* update objet
*/
public void update(String desc,String header)
{
Session session = null;
try
{
session = createSession();
Product prod = new Product();
prod.setProductDesc(desc);
prod.setProductName(header);

//add
session.beginTransaction();
session.saveOrUpdate(prod);
session.connection().commit();
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
finally
{
try
{
session.close();
}
catch(HibernateException e)
{
System.out.println("trying to close " + e.toString());
}
}
}

/**
* create session
*
* @return session current session
*/
public Session createSession() throws MappingException,HibernateException
{
Configuration cfg = new Configuration().configure();
cfg.addClass(com.hibernate.beans.Product.class);
SessionFactory sessions = cfg.buildSessionFactory();
return sessions.openSession();
}
}

The line that is throwing NullPointerException is Configuration initialization. I tried using Environment Class to get init properties, both hibernate.properties and hibernate.cfg.xml are in the classpath, it gives me system properties and not hibernate parameters. I have no idea what could have gone wrong, but by looking at hibernate Configuration source code I think that it is not reading initial parameters.
Thanks for your help.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


The line that is throwing NullPointerException is Configuration initialization.

Not according to your stack trace, which seems to say the error is at line 49: session.close(); If it was the Configuration which was null the exception should be thrown from line 65. What do you see in the StdOut while you run the test? Hibernate usually logs the configuration and mapping stuff, so you should be able to see quite clearly if its ok. What happens to your session between opening it and trying to close it I couldn't guess.

One thing: Why are you calling commit() against the connection and not the session? I assume its because you have disabled auto-commit mode, otherwise this will cause problems.
[ May 26, 2004: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
If that is your full stack trace, then I don't believe the problem is with your Hibernate configuration because, if it was, the top item in the stack would be some net.sf.hibernate... method. But your stack trace stops with your update() method. Is that the full stack trace?

Couple of things I noticed that may or may not help.
1. Since your Session is acquired within the try {}, when you close it in the finally block, you need to make sure it is not null, e.g. if (session != null) session.close(); Based on your stack trace, I suspect that this is actually what's causing the NullPointerException.

2. It looks like you are mixing-up transaction usage. You start a hibernate transaction using their transaction api, but then try to commit the transaction using the actual Connection object. If you want to use Hibernate's transaction api, then I believe you should change your code to look something like this:
3. I realize the createSession() method was just for testing purposes and your "real" app may not use it, but I can't help commenting that you should only build your Configuration and SessionFactory once and store the SessionFactory somewhere your client code can access it to obtain new SessionS.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Roman Merkushen
Greenhorn

Joined: May 25, 2004
Posts: 9
Thank you for your help. I fixed the code and realized that dom4j-1.4.jar was not in the classpath.
However, I am still getting runtime errors :
0 [main] INFO cfg.Environment - Hibernate 2.1.3
0 [main] INFO cfg.Environment - hibernate.properties not found
16 [main] INFO cfg.Environment - using CGLIB reflection optimizer
16 [main] INFO cfg.Configuration - configuring from resource: /hibernate.cfg.xml
16 [main] INFO cfg.Configuration - Configuration resource: /hibernate.cfg.xml
16 [main] WARN cfg.Configuration - /hibernate.cfg.xml not found
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
at net.sf.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:849)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:873)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:860)
at com.hibernate.test.Runner.createSession(Runner.java:76)
at com.hibernate.test.Runner.update(Runner.java:33)
at com.hibernate.test.Runner.main(Runner.java:20)

Both xml and properties files are in my classpath. This is a test program and I am trying to get it to work. Can session factory be stored in context and is there an easy way to configure it through hibernate config file?
Thanks for your help.
Roman.
Loren Rosen
Ranch Hand

Joined: Feb 12, 2003
Posts: 156
IIRC the configuration file(s) have to be in the root directory of one of the jar files in your search path. Or use one of the other methods in the Configuration class instead of configure().
Roman Merkushen
Greenhorn

Joined: May 25, 2004
Posts: 9
Thank you,
I solved it with
Configuration cfg = new Configuration().configure(new File("d:/work/Hibernate/classes/hibernate.cfg.xml"));
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by Loren Rosen:
IIRC the configuration file(s) have to be in the root directory of one of the jar files in your search path. Or use one of the other methods in the Configuration class instead of configure().


This point was very clear in the documentation :roll: ...not. I was putting the file in the classpath, not the root of a jar in the classpath. The file solution looks good too however that is problematic from a war.

Thx
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Originally posted by Loren Rosen:
IIRC the configuration file(s) have to be in the root directory of one of the jar files in your search path.


what do you mean, "root directory of .. jar file... search path" ? And I am trying to use absolute file address , still doesn't work.

I am using Eclipse 3.0.

Thanks
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
In this way I "solve" the problem.

I create a new folder "config", and put configuration and mapping file into it. and write ant build file. It can work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate NullPointerException