Bug 33446 - NullPointerException in org.apache.tools.ant.XmlLogger.encode()
Summary: NullPointerException in org.apache.tools.ant.XmlLogger.encode()
Status: RESOLVED FIXED
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.6.2
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 1.6.3
Assignee: Ant Notifications List
URL:
Keywords: PatchAvailable
Depends on:
Blocks:
 
Reported: 2005-02-08 16:34 UTC by Robert Enyedi
Modified: 2008-02-22 12:18 UTC (History)
0 users



Attachments
This solves the problem for me (686 bytes, patch)
2005-02-08 16:37 UTC, Robert Enyedi
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.