java - 线程 “main”中的异常java.lang.NullPointerException

标签 java compiler-errors nullpointerexception

这个问题已经在这里有了答案:




已关闭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/

相关文章:

java - PublicKey/PrivateKey 和 RSAPublicKey/RSAPrivateKey 有什么区别?

java - 从 TomEE 中的另一个 Web 应用程序访问单例 EJB

c++ - 从匹配返回值类型的映射中返回值

java - 绘制 Rectangle2D 数组时出现空指针异常

java - 如何在java中的Lotus Notes中获取example@abc.com中的电子邮件ID而不是 "CN= example/OU= Server/O=company"

java - 使用 JUnit 5 的 Assert 类的编译错误

c++ - 无法将复数 <double> 转换为 double

java - android studio如何解决编译错误?

java - 简单 JOGL 测试用例中的 NullPointerException

java - 为什么在模拟后返回对象时会出现 NullPointerException?