Bug 11570

Summary: XML Appender throws NullPointerException when logging null
Product: Log4j - Now in Jira Reporter: David Vandegrift <dvandegrift>
Component: AppenderAssignee: log4j-dev <log4j-dev>
Status: RESOLVED FIXED    
Severity: critical CC: vkorenev
Priority: P3    
Version: 1.2   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Attachments: Here is a simple patch to log the string "null" instead of throwing a java.lang.NullPointerException

Description David Vandegrift 2002-08-08 20:24:30 UTC
Overview Description:

When calling log.error(null), a NullPointerException is thrown.  See the stack
trace below.

Of course, I wasn't calling log.error(null) in my code.  Rather, I was logging
an exception message as such:  
   log.error(e.getMessage())  <-- triggered exception
But the exception message was null so log4j threw an NullPointerException.

Steps to Reproduce:
1. Setup the following XML Appender section in your log4j.properties file.
######## XML SECTION ##################
# XML is a RollingFileAppender 
#######################################
log4j.appender.XML=org.apache.log4j.RollingFileAppender
log4j.appender.XML.file=${base-path}logs/mgr_log.xml
log4j.appender.XML.MaxFileSize=20MB
log4j.appender.XML.MaxBackupIndex=5
log4j.appender.XML.layout=org.apache.log4j.xml.XMLLayout
log4j.appender.XML.append=true

2. Create a class with the following line:
   log.error(null)

3. Or use the following try/catch block
   try {
       throw new Exception();
   }
   catch (Exception e) {
       log.error(e, e);              // works as expected
       log.error("bug found", e);    // works as expected
       log.error(e.getMessage());    // throws NullPointerException
       log.error(e.getMessage(), e); // throws NullPointerException
   }


Actual Results:
A NullPointerException is thrown as represented in the following stack trace:

######### Stack Trace ###############
java.lang.NullPointerException
 at org.apache.log4j.helpers.Transform.appendEscapingCDATA(Transform.java:74)
 at org.apache.log4j.xml.XMLLayout.format(Unknown Source)
 at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:292)
 at org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:225)
 at org.apache.log4j.WriterAppender.append(WriterAppender.java:150)
 at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221)
at
org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57)
 at org.apache.log4j.Category.callAppenders(Category.java:187)
 at org.apache.log4j.Category.forcedLog(Category.java:372)
 at org.apache.log4j.Category.error(Category.java:286)
 at com.bluearc.mgr.screens.Index.doThroughput(Index.java:253)


Expected Results:
An XML formatted message should have been logged to mgr_log.xml with an empty
CDATA section.  Similar to:
<log4j:message>![CDATA[]]</log4j:message>
or
<log4j:message></log4j:message>


Build & Platform:
build: Log4j version 1.2.5 (release ver)
platform: RedHat 7.2 & Windows 2000 with JDK 1.3.1_02


Additional Builds and Platforms:
 - Occurs On
        Log4j version 1.2.6


Additional Information:
 - xerces1_4_4.jar and xalan-j_2_4_D1.jar in classpath.
Comment 1 Hendrik Brummermann 2002-09-10 14:21:34 UTC
Created attachment 2991 [details]
Here is a simple patch to log the string "null" instead of throwing a java.lang.NullPointerException
Comment 2 Ceki Gulcu 2003-02-18 16:45:19 UTC
Fixed in 1.2.8. A null string will be returned as "", (the empty string).
Comment 3 Ceki Gulcu 2003-02-18 16:46:06 UTC
*** Bug 16170 has been marked as a duplicate of this bug. ***