Solved

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

Posted on 2004-08-27
14
3,619 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
Comment Utility
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
Comment Utility
Can you post the rest of the stack trace?
0
 

Author Comment

by:HycelTaylor
Comment Utility
That's all of the trace.  I'm not sure why it's saying ... 16 more
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:HycelTaylor
Comment Utility
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
Comment Utility
pat5star,

Good advice.  Thanks.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
> 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
Comment Utility
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
Comment Utility
yes using system properties only really practical for fairly simple apps.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
8-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 75
computer science syllabus 3 52
Java asynchronous logging 4 31
eclipse java  build path 6 18
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now