server错误:JSF2.2 Web应用程序中的类java.lang.NullPointerException

时间:2013-11-17 10:53:30

标签: jsf

您好,并提前感谢您提供的任何帮助。我的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;
    }

SimpleJavaFileObject

public class CompilerJavaObject extends SimpleJavaFileObject {
     String contents = null;

    public CompilerJavaObject(String sourceName, String contents) throws Exception {
        super(new URI(sourceName), JavaFileObject.Kind.SOURCE);
        this.contents = contents;
    }

    @Override
    public CharSequence getCharContent(boolean encodingErrors) {
        return contents;
    } 
}

的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或部分字符串变量中初始化我的Compiler对象是错误的吗? 再次感谢您提供的任何帮助。

0 个答案:

没有答案