在初始化log4j文件时获得异常

时间:2011-08-30 15:09:30

标签: eclipse hibernate logging apache-commons-logging

当尝试从配置文件初始化hibernate时,我得到NullPointerException .. 根本原因显示为

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)

我在类路径中有所有与日志相关的jar和属性文件。

我的hibernate初始化代码是:

   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);
    }

简单的java类不会抱怨与日志相关的错误,例如以下java类没有抱怨..它正确初始化并写入日志文件夹

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;
}

因为它适用于独立的Java类而不适用于执行hibernate初始化的类 - 我更加困惑..任何指针?详细跟踪

        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

发布跟踪时存在对齐问题,对不起abt那个,无法纠正那个..

1 个答案:

答案 0 :(得分:3)

最后,我能够解决这个问题。后来猜测它原来是一个日食配置。在eclipse 3.7中,代码名称为indigo,libs被添加到类路径的方式导致了这个问题..

  • 第一种方式: 调试配置 - &gt; classpath-&gt; bootstarp条目,如果通过选择“添加Jars”选项添加添加库,则此操作无效。
  • 正确的方式: 从window-&gt; preferences-&gt; java-&gt; buildpath-&gt; classpath变量定义类路径变量,比如说HIBERNATE_LIBS并指向它ti libs文件夹 现在,进入Debug Configurations-&gt; classpath-&gt; bootstarp条目,选择“Advanced option”并添加类路径变量。这修复了这个问题。

我想这是特定的3.7,因为我升级了eclipse版本并开始得到这个问题,第一种方式仍然适用于eclipse 3.2