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

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.
HycelTaylorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HycelTaylorAuthor Commented:
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
CEHJCommented:
Can you post the rest of the stack trace?
0
HycelTaylorAuthor Commented:
That's all of the trace.  I'm not sure why it's saying ... 16 more
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

CEHJCommented:
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
HycelTaylorAuthor Commented:
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
CEHJCommented:
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
pat5starCommented:
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
HycelTaylorAuthor Commented:
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
HycelTaylorAuthor Commented:
pat5star,

Good advice.  Thanks.
0
Mick BarryJava DeveloperCommented:
> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HycelTaylorAuthor Commented:
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
Mick BarryJava DeveloperCommented:
yes using system properties only really practical for fairly simple apps.
0
CEHJCommented:
>>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
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.