You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bu...@apache.org on 2006/01/31 00:44:29 UTC

DO NOT REPLY [Bug 38458] New: - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

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: amross@stagingconnections.com


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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From jkf@apache.org  2006-02-02 23:54 -------
Updated with suggested fix, I oversaw the logging of the task itself.

-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From peterreilly@apache.org  2006-02-02 09:50 -------
I am afraid not, This would cause so many Backword and Forward compatibly
problems, it would not be funny.

All most ant objects live in a Project context. A Project context provides a large
number of servies - properties, location of build file, references, tasks/type etc.
Just creating a project in a ant object constructor is not sufficient.

One way to look at project would be as a kind of ejb container.

Perhaps every task should enforce the need to have the project
in the constructor. However ant objects normally do not enforce having
the construtor make a valid object. Doing this would make
xml -> java reflection configured objects impossible.

In any case, ant code changes are *very* conservative. Normally
a public method or constructor is not allowed to be removed or changed
in new releases of ant.

-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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


jkf@apache.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Additional Comments From jkf@apache.org  2006-02-02 21:09 -------
Fixed for logging by falling back to the baseclass logging mechanism. Note using
tasks outside a project will not always work. Now at least it will not break
just because of logging.

-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From stevel@apache.org  2006-02-01 11:05 -------
There are lots of ant tasks out there that assume that getProject()!=null. we
can (and will) patch the logging, but you do need to create a project. It
represents the core context of a set of running ant tasks: logging, properties,
datatypes.

If there is bits of the docs on embedding ant in other apps that you think
should be improved, we will happily take those patches on. 

-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From amross@stagingconnections.com  2006-01-31 00:45 -------
Created an attachment (id=17542)
 --> (http://issues.apache.org/bugzilla/attachment.cgi?id=17542&action=view)
Patch to apply to org.apache.tools.ant.Task


-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From gudnabrsam@yahoo.com  2006-02-02 06:02 -------
(In reply to comment #5)
> If that is the case, that would ideally be enforced in the constructor. 
Perhaps
> the constructor could create a "default" project in the project field to 
ensure
> the Task always has a project associated with it, for these kind of cases?

Hmm... at first I was like, what?  But if there were a static 
Project.DEFAULT_INSTANCE Project, whose only properties were System properties, 
etc... hmmm...

-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From amross@stagingconnections.com  2006-02-02 00:02 -------
(In reply to comment #4)
> ... you do need to create a project. It
> represents the core context of a set of running ant tasks: logging, properties,
> datatypes.

If that is the case, that would ideally be enforced in the constructor. Perhaps
the constructor could create a "default" project in the project field to ensure
the Task always has a project associated with it, for these kind of cases?

> If there is bits of the docs on embedding ant in other apps that you think
> should be improved, we will happily take those patches on. 

I will take a look. It's hard to work out where to put these kind of 'gotchas'. 

Thanks for taking on this patch. I appreciate it.
Andreas.



-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From gudnabrsam@yahoo.com  2006-02-02 15:13 -------
(In reply to comment #8)
> I am afraid not, This would cause so many Backword and Forward compatibly
> problems, it would not be funny.

Come on, Peter, ANYTHING can be funny if you try hard enough...

-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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


jkf@apache.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |1.7




-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From amross@stagingconnections.com  2006-02-02 07:45 -------

> Hmm... at first I was like, what?  But if there were a static 
> Project.DEFAULT_INSTANCE Project, whose only properties were System properties, 
> etc... hmmm...

I am totally not familiar with the Ant codebase, but...
I was thinking the constructor would just do a 

setProject(new Project());

instead of me having to mutter the mystical incantation

XSLTProcess process=new XSLTProcess();
process.setProject(new Project());

which is what I am doing now to get things working. Or as you suggest it could
use a static Project instance, if that works better.

It just seems to me that either
1) Every Task has a Project, in which case the Task class should enforce that 
or
2) a Project is optional, and subclasses of Task should not assume there is one.


-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From amross@stagingconnections.com  2006-01-31 23:12 -------
(In reply to comment #2)
> This has historically been considered "your problem."  That said, the patch
seems fine.

That's cool. I *was* able to get around this by calling setProject(), once I had
figured out what was going on, so it's not a showstopper. I had to download the
source and hunt around a bit to get to this point though. It's always much
easier and saves a lot of time if you get an informative error message (e.g. 
"IllegalStateException: setProject() must be called before using this class" or
whatever) instead of a random NullPointerException. Often if I am trying out an
open source library and it gives me an internal NullPointerException I'll throw
it away and try something else. Mind you, this is *Ant*, not some random open
source library, so you can get away with that I guess :-)


-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


DO NOT REPLY [Bug 38458] - [PATCH] NullPointerException thrown in Task.log() if setProject() has not been called

Posted by bu...@apache.org.
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





------- Additional Comments From gudnabrsam@yahoo.com  2006-01-31 15:40 -------
This has historically been considered "your problem."  That said, the patch
seems fine.

-- 
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: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org