0

When trying to initialize hibernate from configuration file, I get NullPointerException .. Root cause is shown as

Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)

I have all the logging related jars and properties files in class path.

My hibernate initialization code is :

   private static final SessionFactory sessionFactory;
private static final ThreadLocal<Session> hibernateSession=new ThreadLocal<Session>();

static{
    try{
        sessionFactory=new Configuration().configure("llhs_hibernate.cfg.xml").buildSessionFactory();
        System.out.println("done");
    }catch (HibernateException he){
        he.printStackTrace();
        throw new ExceptionInInitializerError(he);
    }catch (Exception e){
        e.printStackTrace();
        throw new ExceptionInInitializerError(e);
    }

Simple java classes does not complain about log related error, for example following java class does not complain ..It properly initializes and writes to logs folder

public class MetadataService implements Serializable {
private static final Logger logger = LogManager.getLogger(MetadataService.class);
private static final long serialVersionUID = 1L;
public MetaData  getMetaData(){
    logger.info("Info");
    try {
        //some code here
    } catch (Exception e1) {
        logger.error("Error");
    }

    return metaData;
}

Since it works for stand alone Java class and not for class doing hibernate initialization --I am more perplexed ..Any pointer? Detailed trace

        java.lang.ExceptionInInitializerError
    at com.llhs.persistence.impl.db.hibernate.utils.HibernateUtils.<clinit>(HibernateUtils.java:17)
    at com.llhs.persistence.impl.hibernate.HibernateDbImplTest.addStudentDetails(HibernateDbImplTest.java:61)
    at com.llhs.persistence.impl.hibernate.HibernateDbImplTest.testAddFindStudentDetails(HibernateDbImplTest.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException(Caused by java.lang.NullPointerException))
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:126)
    ... 26 more
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
    at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
    ... 30 more
Caused by: java.lang.NullPointerException
    at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
    ... 31 more

There is alignment issue while posting trace, sorry abt that, not able to correct that one..

Robin Green
  • 32,079
  • 16
  • 104
  • 187
cpandey05
  • 1,241
  • 2
  • 17
  • 30
  • Not enough stacktrace in the exception, please give it all. Anyway, it's very bad idea to use static singletons here, use the Spring or similar for dependency injection. Also, the hibernate is moaning about apache commons, not about the jul as in your test case. I suspect some problems with class loaders and static initialization blocks in other classes. – kan Aug 30 '11 at 15:47
  • Yes, I completely agree, but this is just some test code .. use n throw kind of.. so ignoring design aspects. By the way i donno think its due to static block, as I have used this in other env. And yes, its an issue in apache commons, so I tagged it as logging as well – cpandey05 Aug 30 '11 at 19:01
  • I added eclipse as a tag, this problem is very specific while using eclipse to run test suite. If ant script is used, this does not occur. – cpandey05 Aug 31 '11 at 06:22

1 Answers1

3

Finally, I was able to solve this. As later guessed it turned out to be an eclipse configuration. In eclipse 3.7, code name indigo, the way libs were being added to the classpath caused this issue ..

  • First way: Debug Configurations->classpath->bootstarp entries, if Add libs added by selecting "Add Jars" options, THIS DOES NOT Work.
  • Correct WAY: Define a classpath variable from window->preferences->java->buildpath->classpath variable, say HIBERNATE_LIBS and point it ti libs folder Now, got to Debug Configurations->classpath->bootstarp entries, select "Advanced option" and add the class path variable. This FIXED the issue.

I guess this is specific to 3.7, as I upgraded the eclipse version and started getting this issue, First way still works in eclipse 3.2

cpandey05
  • 1,241
  • 2
  • 17
  • 30
  • Correct way is to use the Maven ;) – kan Aug 31 '11 at 07:20
  • Yes I use that in production (Not maven, but ant) and as mentioned it was running fine as well.. just as ease of use I tried this from eclipse .. and it broke .. was just curios to fix it up :) – cpandey05 Aug 31 '11 at 18:37
  • 2
    You should use Maven, it's not the same as ant, it's more high-level tool for dependency management. The maven project could be imported in the Eclipse to build all classpaths/etc stuff for you automatically, avoiding human errors. – kan Sep 01 '11 at 07:38