Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.NullPointerException when trying to compile

843810Jul 23 2008 — edited Jul 29 2008
Hi everybody!

I'm trying to compile a file using javax.tools.JavaCompiler but I get a java.lang.NullPointerException when running the program.

Here is my code:
public boolean CompExecFile(File filename){

		boolean compRes = false;

		try {

			System.out.println(filename.getAbsolutePath());
			JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
			StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); //line where I get the error
			Iterable<? extends JavaFileObject> compilationUnits2 = fileManager.getJavaFileObjects(filename);
			compRes = compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();
			fileManager.close();
	        if (compRes) {
	            System.out.println ("Compilation was successful");
	        } else {
	            System.out.println ("Compilation failed");
	        }
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		return compRes;

	}
There error on the line "StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);":
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException+

I've seen several examples using the parameters "null,null,null" but I don't know if it's good :(

Many thanks for your help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2008
Added on Jul 23 2008
18 comments
2,466 views