Bug 33446

Summary: NullPointerException in org.apache.tools.ant.XmlLogger.encode()
Product: Ant Reporter: Robert Enyedi <renyedi>
Component: CoreAssignee: Ant Notifications List <notifications>
Status: RESOLVED FIXED    
Severity: normal Keywords: PatchAvailable
Priority: P3    
Version: 1.6.2   
Target Milestone: 1.6.3   
Hardware: PC   
OS: Windows XP   
Attachments: This solves the problem for me

Description Robert Enyedi 2005-02-08 16:34:48 UTC
I found this problem while automating my build process. I am using CruiseControl
2.2 which outputs logs in XML format and uses Ant's XML logger.

This is my stack trace:

java.lang.NullPointerException
        at
org.apache.tools.ant.util.DOMElementWriter.encode(DOMElementWriter.java:174)
        at
org.apache.tools.ant.util.DOMElementWriter.write(DOMElementWriter.java:95)
        at
org.apache.tools.ant.util.DOMElementWriter.write(DOMElementWriter.java:113)
        at org.apache.tools.ant.XmlLogger.buildFinished(XmlLogger.java:189)
        at org.apache.tools.ant.Project.fireBuildFinished(Project.java:1796)
        at org.apache.tools.ant.Main.runBuild(Main.java:693)
        at org.apache.tools.ant.Main.startAnt(Main.java:188)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:196)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:55)

The line the exception occurs is:

int len = value.length();

"value" being the method's argument.

The fix seems obvious by checking value for null and in this case initializing
len with 0:

int len = (value==null ? 0 : value.length());

This worked for me.
Comment 1 Robert Enyedi 2005-02-08 16:37:29 UTC
Created attachment 14209 [details]
This solves the problem for me
Comment 2 J.M. (Martijn) Kruithof 2005-02-08 23:17:07 UTC
Something logging in XML should know that it is illegal to omit a value from an
attribute.

The XML Logger adds the following attributes:

        buildElement.element.setAttribute(TIME_ATTR,
                DefaultLogger.formatTime(totalTime));

            buildElement.element.setAttribute(ERROR_ATTR,
                    event.getException().toString());

An Exception, being a Throwable MUST NOT return null on toString, as this
breaches the contract of toString on a Throwable, formatTime should not be
returning null either.

How can this error be reproduced (Or could you adapt XmlLogger to give some info
 in case the event.getException().toString() returns null, to know which
exception class is broken.)

To check this please replace the start of the buildFinished method in the
XmlLogger class:

    public void buildFinished(BuildEvent event) {
        long totalTime = System.currentTimeMillis() - buildElement.startTime;
        buildElement.element.setAttribute(TIME_ATTR,
                DefaultLogger.formatTime(totalTime));

        if (event.getException() != null) {
            String temp = event.getException().toString();
            temp = ((temp != null) ? temp : 
"Throwable with incorrect toString(): " +
event.getException().getClass().toString());

            buildElement.element.setAttribute(ERROR_ATTR,
                    temp);
            // print the stacktrace in the build file it is always useful...
            // better have too much info than not enough.
            Throwable t = event.getException();
            Text errText = doc.createCDATASection(StringUtils.getStackTrace(t));
            Element stacktrace = doc.createElement(STACKTRACE_TAG);
            stacktrace.appendChild(errText);
            buildElement.element.appendChild(stacktrace);
        }
Comment 3 Peter Reilly 2005-02-09 11:09:05 UTC
I think that this is fixed with the fix to bug 31840. (a
task name was not set and was used as an attribute value
without checking).
Can you check with the lastest cvs version of ant.
It may be good to use the patch here for safely sake,
so I am leaving this bug open. 
Comment 4 Stefan Bodewig 2005-09-07 21:04:10 UTC
let's assume Peter is correct again.