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?

Joetjah
  • 6,292
  • 8
  • 55
  • 90
SuUbha
  • 167
  • 2
  • 15
  • compiler is null? From the api docs for ToolProvider.getSystemJavaCompiler() "the compiler provided with this platform or null if no compiler is provided" – Rob Garwood Aug 08 '13 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. – amit Aug 08 '13 at 13:53
  • 2
    http://stackoverflow.com/questions/2543439/null-pointer-exception-while-using-java-compiler-api – Luca Basso Ricci Aug 08 '13 at 13:53

5 Answers5

4

From ToolProvider.getSystemJavaCompiler():

Returns:

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

arshajii
  • 127,459
  • 24
  • 238
  • 287
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.

0

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

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

Algorithmist
  • 6,657
  • 7
  • 35
  • 49
  • your compiler object is null? An *object* cannot be null. A variable can have a `null` value. – amit Aug 08 '13 at 13:52
  • 3
    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. – zapl Aug 08 '13 at 14:00
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

Ryan Sayles
  • 3,389
  • 11
  • 56
  • 79
  • 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... – SuUbha Aug 09 '13 at 19:14
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

Kazaag
  • 2,115
  • 14
  • 14