You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Thomas Schapitz <sh...@ePost.de> on 2004/08/07 15:38:56 UTC

Probable Bug (NPE) and Fix in class oata.Project?

Hi all,

When I'm using Ant 1.6.1 in Netbeans 3.6 to execute one of the emma-tasks
(see: emma.sourceforge.net), I'm seeing the following stacktrace:

<preceding tasks left out....>
instrument:
java.lang.NullPointerException
        at org.apache.tools.ant.Project.getThreadTask(Project.java:1985)
        at org.apache.tools.ant.Project.demuxFlush(Project.java:1155)
        at 
org.apache.tools.ant.DemuxOutputStream.processFlush(DemuxOutputStream.java:186)
        at 
org.apache.tools.ant.DemuxOutputStream.flush(DemuxOutputStream.java:211)
        at java.io.PrintStream.flush(PrintStream.java:136)
        at 
sun.nio.cs.StreamEncoder$CharsetSE.implFlush(StreamEncoder.java:410)
        at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:152)
        at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:213)
        at java.io.BufferedWriter.flush(BufferedWriter.java:230)
        at java.io.PrintWriter.flush(PrintWriter.java:120)
        at com.vladium.logging.Logger.cleanup(Logger.java:436)
        at com.vladium.logging.Logger.pop(Logger.java:364)
        at com.vladium.emma.Processor.run(Processor.java:60)
        at com.vladium.emma.instr.instrTask.execute(instrTask.java:77)
        at com.vladium.emma.emmaTask.execute(emmaTask.java:57)
        at 
org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:269)
        at org.apache.tools.ant.Task.perform(Task.java:364)
        at org.apache.tools.ant.Target.execute(Target.java:301)
        at org.apache.tools.ant.Target.performTasks(Target.java:328)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1215)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1063)
        at 
org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:178)
        at 
org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:252)
        at 
org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:125)

This seems to happen only in Netbeans, not when I'm calling ANT (1.6.0, 
or 1.6.2)
from the commandline. (didn't try 1.6.1)

The relevant section in oata.Project is:

   /**
     * Get the current task associated with a thread, if any
     *
     * @param thread the thread for which the task is required.
     * @return the task which is currently registered for the given 
thread or
     *         null if no task is registered.
     */
    public Task getThreadTask(Thread thread) {
        Task task = (Task) threadTasks.get(thread); <--- NPE occurs here.
        if (task == null) {
            ThreadGroup group = thread.getThreadGroup();
            while (task == null && group != null) {
                task = (Task) threadGroupTasks.get(group);
                group = group.getParent();
            }
        }
        return task;
    }

Fixing this to read.
    public Task getThreadTask(Thread thread) {
        Object task = threadTasks.get(thread);  // removed cast
        if (task == null) {
            ThreadGroup group = thread.getThreadGroup();
            while (task == null && group != null) {
                task = (Task) threadGroupTasks.get(group);
                group = group.getParent();
            }
        }
        return (Task) task;
    }

Solved the problem for me.
Looks to me, as if somebody was aware, that the threadTask.get() might 
return null,
but misplaced the cast to java.lang.Task.

As a side thought, I'm wondering, wether this might happen to
threadGroupTask.get() also, so it would be better to remove the cast 
there also....

Cheers!
Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org