Interesting Problem with log4j, Tomcat and NullPointerExceptions

log4j.jpg

I ran into a very interesting problem today with NullPointerExceptions and log4j. Specifically, it seemed that I was getting NullPointerExceptions on calls to log4j. I couldn't believe it at first, and I did several things to "clear up" the error, but the NullPointerException persisted on the statement:

  log.info("Time to clear the daily data.");

and in theory, that shouldn't happen when we defined at the top of the class the value for log:

  protected static Log  log = LogFactory.getLog("my.package.ClassName");

It should just always be there. Very odd.

So I hit Google and got a really good hint from one of the answers there

...when using Tomcat, it will unload the ClassLoader that held the webapp, and if you 'un-deploy' the webapp, and maintain the thread, it'll keep going. However, it's lost it's static variables in the unloading and now you're going to get a NullPointerException.

Since there is no way to close out the class that's loaded and instantiated for an H2 trigger, I had been just shutting down the chat interface of the alerts. But what I needed to do was to kill the alert thread(s), and then stop them for good, and clear all references so that they could be cleaned up.

So I did just that. I'm thinking that this is going to make a big difference in the overnight roll-over, as we'll have shut everything down and if the class loader had been dropped, there's no chance of it making a call and getting a NullPointerException.