2012-12-22 97 views
-1

Possible Duplicate:
Null Pointer Exception while using Java Compiler API异常线程“main”显示java.lang.NullPointerException

我有这个计划的一些问题。

错误:

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"); 
     } 
     } 
    } 

回答

0

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.

+0

我想说的是,将三个空参数传递给一个函数并得到一个'NullPointerException'?嗯,让我们想想这个... – ApproachingDarknessFish

相关问题