Solved

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

Posted on 2004-08-27
14
3,705 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

831 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