• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Daisy Smith
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I have a file(GeneratedClass.java) which is in AST form and I want it to get picked up by javac for compilation. I have tried to add the file to the compilation units and then let javac do the rest but I get NullPointerExceptions at lines 59 and 48. This is what I have done so far:


Any help is appreciated.
Thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daisy Smith wrote:but I get NullPointerExceptions at lines 59 and 48.

I broke up a few of your long lines because they made your thread difficult to read.

It would also be useful if you could provide us with the exact stacktrace (maybe just the first 10 lines or so if it's big) so we can see the complete flow.

PS: Don't put it inside code blocks; again, because those lines tend to get quite long.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daisy Smith wrote:Any help is appreciated.

About the only thing I can see at the moment is that 't' is only set in your parse() method if filename.getName().endsWith(".java") return true. However, since it would appear that filename may actually be the thing that's null, I'm not sure that this is your current problem.

Like I say, a complete stacktrace will help us out immensely.

Winston
 
Daisy Smith
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS: Don't put it inside code blocks; again, because those lines tend to get quite long.

Noted!
Thanks Winston.

Like I say, a complete stacktrace will help us out immensely.

Here it is:

run:
Exception in thread "main" java.lang.NullPointerException
at myclass.MyClass.parse(MyClass.java:59)
at myclass.MyClass.main(MyClass.java:48)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are setting t to null, then parsing it if....
If that whatever is false it will not be replaced by whatever is parsed and will remain null. Then when you write t.anything even if that anything is spelt e-n-d-P-o-s-i-t-i-o-n-s you get an Exception.

Maybe you meant if (t != null)?? But there is something wrong with having to test for nullity like that. There must be some other way to prevent nulls there. Start by ensuring that the file name can only end .java.
 
Daisy Smith
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Campbell.
Then when you write t.anything even if that anything is spelt e-n-d-P-o-s-i-t-i-o-n-s you get an Exception.

I would really like this particular case to work therefore replaced everything in the public JCTree.JCCompilationUnit parse(JavaFileObject filename)
block to return super.parse(filename).

When I run it , I get the following stacktrace:

run:
Exception in thread "main" java.lang.NullPointerException
at com.sun.tools.javac.main.JavaCompiler.readSource(JavaCompiler.java:571)
at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:632)
at myclass.MyClass.parse(MyClass.java:56)
at myclass.MyClass.main(MyClass.java:48)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

This link shows the JavaCompiler that MyClass extends which makes me believe that perhaps, the problem is with the variable compilationUnits which I pass to parse(filename) at line 48.
Why isn't the file being read?
Perhaps, there is a problem with the value to which compilationUnits is being assigned?
It gets stdFileManager.getJavaFileForInput(SOURCE_PATH, "GeneratedClass", SOURCE).

From the javadoc:
public JavaFileObject getJavaFileForInput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind) throws IOException
Gets a file object for input representing the specified class of the specified kind in the given location.
Parameters:
location - a location
className - the name of a class
kind - the kind of file, must be one of SOURCE or CLASS

Am I representing compilationUnits in the wrong way?
The path to GeneratedClass.java is /home/guestu/GeneratedClass.java and I have assumed that my SOURCE_PATH is /home/guestu.
Thanks again for your replies.
 
Daisy Smith
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is an API problem. I think I will move the question to the JSE/JEE API forum instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!