Solved

Getting a java.lang.NullPointerException using Log4J Logger.getLogger(AJavaClass.class);

Posted on 2004-08-27
14
3,899 Views
Last Modified: 2008-01-09
Hello,

I'm getting a NPE using Log4J in my class.  I initialize a Logger near the beginning of the following class:

public abstract class ServiceConnection {
...
  protected Connection connection;
  static Logger logger = Logger.getLogger(ServiceConnection.class);
...

}

I get the following exception:

java.lang.ExceptionInInitializerError
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:130)
      at org.apache.log4j.PropertyConfigurator.class$(PropertyConfigurator.java:76)
      at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:631)
      at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:602)
      at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:499)
      at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:405)
      at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:431)
      at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:456)
      at org.apache.log4j.LogManager.<clinit>(LogManager.java:145)
      at org.apache.log4j.Logger.getLogger(Logger.java:94)
      at com.tss.testadmin.service.ServiceConnection.<clinit>(ServiceConnection.java:32)
      at com.tss.testadmin.service.ServiceFactory.getSevletManagedService(ServiceFactory.java:57)
      at com.tss.testadmin.service.ServiceFactory.getService(ServiceFactory.java:49)
      at com.tss.testadmin.service.ServiceFactoryTest.runTest(ServiceFactoryTest.java:18)
      at com.tss.testadmin.service.ServiceFactoryTest.main(ServiceFactoryTest.java:32)
Caused by: java.lang.NullPointerException
      at org.apache.log4j.Layout.<clinit>(Layout.java:25)
      ... 16 more
Exception in thread "main"
Process finished with exit code 1

I know for sure that, static Logger logger = Logger.getLogger(ServiceConnection.class);, is the line that I'm crashing on.

Is this not the way I should be using the Logger?
Does this have anything to do with the fact that the class is abstract?

Thanks in advance for any help that I may get in finding a solution concerning this issue.
0
Comment
Question by:HycelTaylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
14 Comments
 

Author Comment

by:HycelTaylor
ID: 11915871
I would like to add that to my current issue.  I commented out all of the Logger commands in the ServiceFactory class and ran the program again.  This time I got the same exception, but in a different class that is using Logger:

public class LogonDelegate extends ServiceConnection implements Logon {

  static Logger logger = Logger.getLogger(LogonDelegate.class.getName());
...
}

The exception:

java.lang.ExceptionInInitializerError
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:130)
      at org.apache.log4j.PropertyConfigurator.class$(PropertyConfigurator.java:76)
      at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:631)
      at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:602)
      at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:499)
      at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:405)
      at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:431)
      at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:456)
      at org.apache.log4j.LogManager.<clinit>(LogManager.java:145)
      at org.apache.log4j.Logger.getLogger(Logger.java:85)
      at com.tss.testadmin.service.logon.LogonDelegate.<clinit>(LogonDelegate.java:18)
      at com.tss.testadmin.service.ServiceFactory.getSevletManagedService(ServiceFactory.java:57)
      at com.tss.testadmin.service.ServiceFactory.getService(ServiceFactory.java:49)
      at com.tss.testadmin.service.ServiceFactoryTest.runTest(ServiceFactoryTest.java:18)
      at com.tss.testadmin.service.ServiceFactoryTest.main(ServiceFactoryTest.java:32)
Caused by: java.lang.NullPointerException
      at org.apache.log4j.Layout.<clinit>(Layout.java:25)
      ... 16 more
Exception in thread "main"

What ever I'm doing wrong, I think it's at the Log4J level and not any of my specific classes.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11916051
Can you post the rest of the stack trace?
0
 

Author Comment

by:HycelTaylor
ID: 11916191
That's all of the trace.  I'm not sure why it's saying ... 16 more
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

by:CEHJ
ID: 11916228
Determine what your classpath is and make sure you haven't got conflicting versions of log4j, not forgetting to look in your extensions directories
0
 

Author Comment

by:HycelTaylor
ID: 11916419
I've checked to make sure that I have only one log4J-1.2.8.jar.  Still, the result it the same. Infact, any log4J method I call yields the same type of exception.  Example:

  BasicConfigurator.configure();

Will yield the same type of Exception.

java.lang.ExceptionInInitializerError
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:130)
      at org.apache.log4j.PropertyConfigurator.class$(PropertyConfigurator.java:457)
      at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:632)
      at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:603)
      at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:500)
      at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:406)
      at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:432)
      at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:460)
      at org.apache.log4j.LogManager.<clinit>(LogManager.java:113)
      at org.apache.log4j.Logger.getRootLogger(Logger.java:104)
      
                at org.apache.log4j.BasicConfigurator.configure(BasicConfigurator.java:36)
      
                at com.tss.testadmin.resources.ResourceLoader.loadToSystemProperties(ResourceLoader.java:48)
      at com.tss.testadmin.resources.ResourceLoader.<init>(ResourceLoader.java:25)
      at com.tss.testadmin.resources.ResourceLoader.getInstance(ResourceLoader.java:30)
      at com.tss.testadmin.service.ServiceFactory.<clinit>(ServiceFactory.java:26)
      at com.tss.testadmin.service.ServiceFactoryTest.runTest(ServiceFactoryTest.java:18)
      at com.tss.testadmin.service.ServiceFactoryTest.main(ServiceFactoryTest.java:32)
Caused by: java.lang.NullPointerException
      at org.apache.log4j.Layout.<clinit>(Layout.java:25)
      ... 18 more
Exception in thread "main"
Process finished with exit code 1

Maybe it's looking for a root loger?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 11916493
Make sure you've configured it properly since there have been changes, e.g.

"As of release 1.2.8. log4j searches for the file log4j.xml as well as the file log4j.properties during initialization"
0
 
LVL 3

Assisted Solution

by:pat5star
pat5star earned 200 total points
ID: 11917581
Make sure your log4j.properties or log4j.xml file is in the /WEB-INF/classes directory. Here is a basic log4j.properties file I use when developing:

log4j.rootLogger=stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d{MMM.dd-HH:mm} [%C{1}] - %m%n

You keep having a NullPointer thrown at org.apache.log4j.Layout so I'm wondering if it's incorrect in your config file. Try using those 4 lines above as I know they work on my machine.

Hope that helps,

-Pat
0
 

Author Comment

by:HycelTaylor
ID: 11917601
CEHJ,

I think I found the problem.  I don't think it has any thing to do with Log4J.  Log4J was simply the first set of classes to manifest problems because of what I had done.  I created a class that loads an application specific properties file.  I then used the System.setProperties(properties) method to load those properties as system properties.  I think using System.setProperties() was a bad idea.  Instead I am now doing the following:

  private void loadToSystemProperties() {
    try {
      properties = new Properties();

      final InputStream testAdminStream = this.getClass().getResourceAsStream(APPLICATION_PROPERTIES);
      properties.load(testAdminStream);

      for (Enumeration enum = properties.propertyNames(); enum.hasMoreElements(); ) {
        final String name = (String) enum.nextElement();
        final String value = (String) properties.get(name);
        System.setProperty(name,value);
      }

      org.apache.log4j.helpers.Loader.getResource(LOG4J_PROPERTIES);
    } catch (IOException e) {
     ...
    }
  }

Everything is now working.  

However, this begs a question:

Should I load application specific properties in with the system properties and thus access an application property like the following?

  System.getProperty(APP_HOME)

Or should I add a method in my singleton ResourceLoader class such as:

  public String getApplicationProperty(String name) {
    return properties.getProperty(name);
  }

and thus, access application properties like the following?

final String homePath = ResourceLoader.getInstance().getApplicationProperty(APP_HOME);

CEHJ,  what is your opinion?
0
 

Author Comment

by:HycelTaylor
ID: 11917689
pat5star,

Good advice.  Thanks.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 11918441
> Should I load application specific properties in with the system properties and thus access an
> application property like the following?

really depends on your requirements. If you application has fairlt simple config requirements then you can load them into system properties.

Otherwise passing a configuration map in the ctor of classes that require configuration would provid more flexibility, for example if you had two instances of the same class that required different config.

0
 

Author Comment

by:HycelTaylor
ID: 11918485
I would agree more to your later view.  It has occured to me that I might have some classes that acts out side of a given class that sets that set the system properties in advance.  Thus, it would be smarter to use the single instance ResourceLoader.  

Thanks for all the help.
0
 
LVL 92

Expert Comment

by:objects
ID: 11918512
yes using system properties only really practical for fairly simple apps.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11920378
>>Should I load application specific properties in with the system properties

One thing you'd have to be aware of is classloading - if you were to load the properties and merge with the system ones, and then *access* them as system properties, how can you be certain that your 'registry' class has been loaded by the container when the client class needs  the property/properties? If you access them via the registry, a null registry reference would signal that there's a problem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11920408
8-)
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question