简体   繁体   English

类别中的java log4j异常

[英]java log4j exception in Category

In my test application I'm getting the below exception 在我的测试应用程序中,出现以下异常

java.lang.NullPointerException
at org.apache.log4j.CategoryKey.<init>(CategoryKey.java:20)
at org.apache.log4j.Hierarchy.getLogger(Hierarchy.java:252)
at org.apache.log4j.Hierarchy.getLogger(Hierarchy.java:233)
at org.apache.log4j.LogManager.getLogger(LogManager.java:179)
at org.apache.log4j.Category.getInstance(Category.java:514)

Even though I am using the line 即使我正在使用线路

static final Logger logger1 = Logger.getLogger(Test.class);

Can someone help me in resolving this.... 有人可以帮助我解决这个问题吗?

Thanks 谢谢

I believe that I may have the answer to this. 我相信我可能对此有答案。 I stumbled upon this very same stack trace recently when I recompiled an old application. 当我重新编译旧的应用程序时,我偶然发现了同样的堆栈跟踪信息。 I was inadvertently using Java 1.6 to compile my application that was originally built on Java 1.4. 我无意间使用Java 1.6来编译最初基于Java 1.4构建的应用程序。 I then deployed it along with log4j-1.2.8 inside of weblogic 8 container running a Java 1.4 virtual machine. 然后,我将它与log4j-1.2.8一起部署在运行Java 1.4虚拟机的weblogic 8容器中。 So my web app, compiled under javac from 1.6 was dropped into weblogic and I hit the deploy button. 因此,我的Web应用程序(在1.6版的javac下编译)被放到了weblogic中,然后单击了deploy按钮。 Bang! 砰! Got the same stack trace as shown here. 与此处显示的堆栈跟踪相同。

It finally occurred to me that it might be a cross-version issue. 最终我想到这可能是一个跨版本的问题。 As best I can tell, the Java 1.4 VM loaded my newer class and tried to initialize it. 尽我所知,Java 1.4 VM加载了我的较新类并尝试对其进行初始化。 When my newer class dynamically linked to the older log4j library, something in the resolution of class.getName failed to return a valid string and a null was passed down to the Logger class instead of a valid string. 当我的较新类动态链接到较旧的log4j库时,class.getName分辨率中的某些内容无法返回有效字符串,并且将null传递给Logger类而不是有效字符串。

I fixed my issue by forcing my build script to use the java 1.4 compiler. 我通过强制构建脚本使用Java 1.4编译器来解决问题。

I got same exception when my logger name was null: 当我的记录器名称为null时,我遇到了同样的异常:

Logger.getLogger(null);

When I changed looger name to some string or class the problem was resolved: 当我将looger名称更改为某些字符串或类时,问题已解决:

Logger.getLogger(MyClass.class);

or 要么

Logger.getLogger("MyLoggerName");

Odd, I did some searching around and that NPE is caused by a null Category name, but when you specify a Logger.getLogger(Test.class) you should not have that problem. 奇怪的是,我进行了一些搜索,发现NPE是由空的Category名称引起的,但是当您指定Logger.getLogger(Test.class)您应该不会Logger.getLogger(Test.class)这个问题。 I recommend getting the log4j source and debugging it. 我建议获取log4j源并对其进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM