DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUGĀ·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=38458>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED ANDĀ·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=38458

           Summary: [PATCH] NullPointerException thrown in Task.log() if
                    setProject() has not been called
           Product: Ant
           Version: 1.6.5
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Core
        AssignedTo: dev@ant.apache.org
        ReportedBy: [EMAIL PROTECTED]


When calling Ant tasks directly from Java, instead of through Ant, the Task has
no Project associated with it. Unfortunately, some code in Task.java assumes
that project is not null and can throw NullPointerExceptions.

To reproduce:

I found this problem using the XSLTProcess task, but you could get the same
problem in any Task. Here is the code I was running. If you read the code in
Task and TaskComponent you will readily see how a problem can occur in this
situation.

String SOURCE_FILE="Training/Training Documentation.mm";

String DESTINATION_FILE="Training/Training Documentation.html";

String TRANSFORM="c:/programs/freemind/accessories/tohtml.xsl";

org.apache.tools.ant.taskdefs.XSLTProcess process=new XSLTProcess();
File sourceFile=new File(SOURCE_FILE);
process.setIn(sourceFile);
File destination=new File(DESTINATION_FILE);
process.setOut(destination);
process.setBasedir(new File("f:/projects/notes/"));
process.setForce(true);
process.setStyle(TRANSFORM);
process.execute();


The stack trace I get when I run this code is:

java.lang.NullPointerException
        at org.apache.tools.ant.Task.log(Task.java:346)
        at 
org.apache.tools.ant.taskdefs.XSLTProcess.execute(XSLTProcess.java:219)
        at
staging.knowledgebase.DeployDocumentation.createHTMLFiles(DeployDocumentation.java:56)
        at
staging.knowledgebase.DeployDocumentation.deployDoco(DeployDocumentation.java:81)
        at 
staging.knowledgebase.DeployDocumentation.main(DeployDocumentation.java:91)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Exception in thread "main" 
Process finished with exit code 1

Expected result:
The process should have completed without crashing. Also, if it crashed, it
should have given a more meaningful error message than a NullPointerException.

Analysis:
- The NullPointerException is thrown in Task.log. 
getProject().log(this, msg, msgLevel);

- The problem is, getProject() can legally return null.

- It turns out, Task.java overrides the log(String msg, int msgLevel) method in
ProjectComponent.java. The implementation in ProjectyComponent contains a check
if project is null, and if so writes to System.err. Task.java fails to replicate
this null pointer checking.

My patch checks if getProject() returns null, and if so just calls the parent
method in ProjectComponent.

Here is the patch to be applied to org.apache.tools.ant.Task:

--- Task.java   Thu Jun 02 15:19:56 2005
+++ TaskMod.java        Tue Jan 31 10:02:37 2006
@@ -343,7 +343,11 @@
      *                 be logged.
      */
     public void log(String msg, int msgLevel) {
-        getProject().log(this, msg, msgLevel);
+        if(getProject()!=null) {
+            getProject().log(this, msg, msgLevel);
+        } else {
+            super.log(msg,msgLevel);
+        }
     }
 
     /**
@@ -475,3 +479,4 @@
         return wrapper;
     }
 }
+

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to