1

Following is the Java code written in eclipse ide...

package compile;

import java.io.IOException;
import java.util.Arrays;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class Execute {
    public static void main(String args[]) throws IOException, ClassNotFoundException {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(
                diagnostics, null, null);
        Iterable<? extends JavaFileObject> compilationUnits = fileManager
                .getJavaFileObjectsFromStrings(Arrays
                        .asList("F:\\practice java\\project\\KeepingMoreKidsQuiet.java"));
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
                diagnostics, null, null, compilationUnits);
        boolean success = task.call();

        System.out.println(success);
        if(!success)
            System.out.println(diagnostics.getDiagnostics());
        fileManager.close();
    }
}

When I run my Java program, it gives me an error on this line

StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

Error I am getting is:

Exception in thread "main" java.lang.NullPointerException at compile.Execute.main(Execute.java:17)

Can you please tell me how can I solve this error?

3
  • compiler is null? From the api docs for ToolProvider.getSystemJavaCompiler() "the compiler provided with this platform or null if no compiler is provided" Commented Aug 8, 2013 at 13:52
  • Did you step through with a debugger? You can see which object is null, and trying to figure out why. My bet is ToolProvider.getSystemJavaCompiler(); returns null, but make sure. Commented Aug 8, 2013 at 13:53
  • 2
    stackoverflow.com/questions/2543439/… Commented Aug 8, 2013 at 13:53

5 Answers 5

4

From ToolProvider.getSystemJavaCompiler():

Returns:

the compiler provided with this platform or null if no compiler is provided

Sign up to request clarification or add additional context in comments.

2 Comments

how can i get rid of this?
@SuUbha See this
2

This usually comes when you are running program with JRE and not JDK.

IDE like Eclipse detect JRE path and set it as their run path for Java programs. If you are using Eclipse then inside Build Path set new VM with JDK path (default one uses JRE path)

IF you run directly from command prompt then check your classpath.

Comments

0

Possibly your compiler reference is null at this line JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

Try setting %JAVA_HOME%\bin at the start of PATH

2 Comments

your compiler object is null? An object cannot be null. A variable can have a null value.
You could see null itself as a special metaphorical object that can be referenced by variables. There is even the Void class to represent it.
0

Your compiler is null, also just as a design ocd comment: don't do all that work in your main method. Take advantage of methods and classes

1 Comment

Thanks to all..The problem is solved...It happened because the Eclipse IDE was pointing to the jre...I changed it to JDK using the following code: System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02"); used in the main() function...
0

You are most probably running you program with the JRE not the JDK. The ToolProvider will only be able to provide a compiler if you run it with the JDK.

This is the expected behavior

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.