Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
服务器错误:JSF2.2 web应用程序中的类java.lang.NullPointerException_Jsf - Fatal编程技术网

服务器错误:JSF2.2 web应用程序中的类java.lang.NullPointerException

服务器错误:JSF2.2 web应用程序中的类java.lang.NullPointerException,jsf,Jsf,您好,提前感谢您提供的帮助。我的web应用程序是一个使用javax.tools包的动态java编译器。我为您提供了我的代码的简单概述 编译器类: public class Compiler { public Compiler(){ } public String compile() throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

您好,提前感谢您提供的帮助。我的web应用程序是一个使用javax.tools包的动态java编译器。我为您提供了我的代码的简单概述

编译器类:

  public class Compiler {

    public Compiler(){

    }

    public String compile() throws Exception {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        DiagnosticCollector diagCollector = new DiagnosticCollector();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagCollector, null, null);
        File outputFile = new File("C:\\Java Projects\\WA9\\src\\java\\compiledFiles");
        fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(outputFile));
        String sourceCode = "public class Test{}";
        String sourceFile = "Test.java";
        JavaFileObject sourceObject = new CompilerJavaObject(sourceFile, sourceCode);
        Iterable<JavaFileObject> fileObjects = Arrays.asList(sourceObject);
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagCollector, null, null, fileObjects);
        String message = "Compilation Success";
        if (!task.call()) {
            List<Diagnostic> diagErrors = diagCollector.getDiagnostics();
            for (Diagnostic d : diagErrors) {
                message = ("Error: " + d.getLineNumber() + " Cause: " + d.getMessage(null));
            }
        }
        return message;
    }
UserBean

@Named("user")
@SessionScoped
public class UserBean implements Serializable {

    private Compiler compiler;
    private String compilerMessage;

    public UserBean() {
        compiler = new Compiler();
    }

    public void compile() throws Exception {
        compilerMessage = compiler.compile();
    }

    public String getCompilerMessage() {
        return compilerMessage;
    }  
}
在我的index.xhtml页面中,我有:

<h:outputText id="msg" value="#{user.compilerMessage}"/>
<h:commandButton value="Compile">
<f:ajax render="msg" listener="#{user.compile()}"/>
</h:commandButton>

当我按下页面中的“编译”按钮时,会从localhost弹出一条消息,其中包含服务器错误:class.java.lang.NullPointerException,我无法跟踪错误原因。 在bean或某些字符串变量中初始化编译器对象是错误的吗?
再次感谢您提供的帮助。

答案在堆栈跟踪中。这是明智的查找和共享。顺便问一下,您是否暗示当作为独立的Java应用程序与
main()一起执行时,它可以正常工作
method,而不是通过JSF表单?当然不是,我只是不想添加我的JSF页面的其余部分。我看到了堆栈跟踪。问题发生在ToolProvider提供空JavaCompiler对象时。规范说它可能返回JavaCompiler对象或空。我只是不明白为什么它返回空对象。
<h:outputText id="msg" value="#{user.compilerMessage}"/>
<h:commandButton value="Compile">
<f:ajax render="msg" listener="#{user.compile()}"/>
</h:commandButton>