Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-27
14
Medium Priority
?
4,134 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 800 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 800 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 400 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 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