这个问题已经在这里有了答案:
已关闭8年。
Possible Duplicate:
Null Pointer Exception while using Java Compiler API
我对该程序有一些问题。
错误:
Exception in thread "main"
java.lang.NullPointerException at test.SimpleCompileTest.main(SimpleCompileTest.java:9)
程序:
package test;
import javax.tools.*;
public class SimpleCompileTest {
public static void main(String[] args)
{
String fileToCompile = "test" + java.io.File.separator+"MyClass.java";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, fileToCompile);
if(compilationResult==0)
{
System.out.println("Compilation is successful");
}
else
{
System.out.println("Compilation has failed");
}
}
}
最佳答案
Java程序的编译不是那么简单,需要更多的精力。请查看教程和API,例如http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html。根据您的NPE,检查http://docs.oracle.com/javase/6/docs/api/javax/tools/package-summary.html:
Unless explicitly allowed, all methods in this package might throw a NullPointerException if given a null argument or if given a list or collection containing null elements. Similarly, no method may return null unless explicitly allowed.
关于java - 线程 “main”中的异常java.lang.NullPointerException ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14000822/