Bug 76161 - NullPointerException from JavaCore
Summary: NullPointerException from JavaCore
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Ant (show other bugs)
Version: 2.1.2   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-Ant-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-13 09:35 EDT by tim orme CLA
Modified: 2004-10-18 15:29 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tim orme CLA 2004-10-13 09:35:32 EDT
When I run headless and execute the following code, a NullPointerException is 
thrown by JavaCore.getDefaultOptions() via the call to AST.parseCompilationUnit
().

public static void main(String[] args) throws Exception
{
	String line = null;
	StringBuffer buffer = new StringBuffer();
			
	BufferedReader bufferedReader = 
		new BufferedReader(new FileReader("C:\\Projects5.1
\\JDTParser\\ClassToParse.java"));
			
	while((line = bufferedReader.readLine()) != null)
	{
		buffer.append(line);
	}				
	
	try
	{
		AST.parseCompilationUnit(buffer.toString().toCharArray());
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}

Here is the output from execution:

C:\Projects5.1\JDTParser>runant -buildfile example.xml

C:\Projects5.1\JDTParser>echo off

C:\Projects5.1\JDTParser>"C:\Program Files\IBM\WebSphere Studio\Application Deve
loper\v5.1.1\eclipse\jre\bin\java"  -cp "C:\Program Files\IBM\WebSphere Studio\A
pplication Developer\v5.1.1\eclipse\startup.jar" org.eclipse.core.launcher.Main
-application com.ibm.etools.j2ee.ant.RunAnt -data c:\projects5.1\jdtparser -buil
dfile example.xml
HeadlessWorkspaceSettings: INITIAL autoBuild=true maxFile=1048576
HeadlessWorkspaceSettings: TEMP autoBuild=false maxFile=-1
HeadlessWebProjectSettings: HeadlessWebProjectSettings save bypassed (no project
s in workspace)
Buildfile: example.xml

RunTestClass:
     [java] java.lang.NullPointerException
     [java]     at org.eclipse.jdt.core.JavaCore.getDefaultOptions(JavaCore.java
:1545)
     [java]     at org.eclipse.jdt.core.dom.AST.<init>(AST.java:90)
     [java]     at org.eclipse.jdt.core.dom.AST.parseCompilationUnit(AST.java:53
6)
     [java]     at TestClass.main(TestClass.java:44)
     [java]     at java.lang.reflect.Method.invoke(Native Method)
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.jav
a:208)
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava
.java:150)
     [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:443)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:163)

     [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
     [java]     at org.apache.tools.ant.Task.perform(Task.java:341)
     [java]     at org.apache.tools.ant.Target.execute(Target.java:309)
     [java]     at org.apache.tools.ant.Target.performTasks(Target.java:336)
     [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1339)

     [java]     at org.eclipse.ant.internal.core.ant.InternalAntRunner.run(Inter
nalAntRunner.java:571)
     [java]     at org.eclipse.ant.internal.core.ant.InternalAntRunner.run(Inter
nalAntRunner.java:493)
     [java]     at java.lang.reflect.Method.invoke(Native Method)
     [java]     at org.eclipse.ant.core.AntRunner.run(AntRunner.java:464)
     [java]     at com.ibm.ant.extras.RunAnt.run(RunAnt.java:27)
     [java]     at org.eclipse.core.internal.boot.InternalBootLoader.run(Interna
lBootLoader.java:858)
     [java]     at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
     [java]     at java.lang.reflect.Method.invoke(Native Method)
     [java]     at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
     [java]     at org.eclipse.core.launcher.Main.run(Main.java:747)
     [java]     at org.eclipse.core.launcher.Main.main(Main.java:583)
BUILD SUCCESSFUL

BUILD SUCCESSFUL
Total time: 3 seconds
HeadlessWebProjectSettings: HeadlessWebProjectSettings restore bypassed (linksBu
ilderCommand was never saved)
Comment 1 Frederic Fusier CLA 2004-10-13 09:59:06 EDT

*** This bug has been marked as a duplicate of 75969 ***
Comment 2 Frederic Fusier CLA 2004-10-13 11:00:21 EDT
As now you're running in headless mode this is obviously not a duplicate, I've
read it too quickly, sorry for that.
In fact it would have been less confusing to reopen bug 75969...
Comment 3 Olivier Thomann CLA 2004-10-15 15:17:44 EDT
I think you need to define a plugin that requires jdt.core.
Move to Platform/Ant for comments.
Comment 4 Darin Swanson CLA 2004-10-18 15:29:38 EDT
Running a simple Java program via Ant through headless Eclipse will not 
correctly configure the JDT core plugin. JDT is not initialized or even used 
except by your code in this case. The plugin lifecycle methods will not be 
called and result in the NullPointerException.

One way to succeed is to create an Ant task. In the execute() method of your 
Ant task have the code you currently have in main(String[])

Create a plugin that requires JDT Core and contribute your Ant task using the 
Ant core extension points. Add your plugin to the Eclipse set of plugins (use -
clean to have the new plugin discovered). Modify your build file to execute 
your task.