Java 托管Bean不工作:调用NotingMB文件中的save函数时出现空指针异常
我正在使用Eclipse以及jsf和MVC框架。。保存从Xhtml文件调用的bean文件函数时。。它显示错误:notingMB.save上出现空指针异常 代码: xhtml文件 CreateFileDialog.xhtmlJava 托管Bean不工作:调用NotingMB文件中的save函数时出现空指针异常,java,jsf,primefaces,Java,Jsf,Primefaces,我正在使用Eclipse以及jsf和MVC框架。。保存从Xhtml文件调用的bean文件函数时。。它显示错误:notingMB.save上出现空指针异常 代码: xhtml文件 CreateFileDialog.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<h:form id="CreateFileDialogForm" prependId="false">
<p:dialog widgetVar="CreateFileDialogWidget"
id="CreateFileDialogId" height="500" width="500" modal="true"
closable="true" draggable="true" resizable="false" header="Create File">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="Subject" value="#{bundle.subject}"/>
<h:inputText id="Subject" value="#{fileMB.info.subject}" required="true"/>
<h:outputLabel for="fileRefNo" value="File-Ref-No"/>
<h:inputText id="fileRefNo" value="#{fileMB.info.fileRefNo}" required="true"/>
<h:outputText value="Classification" />
<p:selectOneRadio id="Classi" value="#{fileMB.info.classification}">
<f:selectItem itemLabel="Normal" itemValue="Normal" />
<f:selectItem itemLabel="Restricted" itemValue="Restricted" />
<f:selectItem itemLabel="Confidential" itemValue="Confidential" />
<f:selectItem itemLabel="Secret" itemValue="Secret" />
<f:selectItem itemLabel="TopSecret" itemValue="Top Secret" />
</p:selectOneRadio>
<h:outputText value="Priority" />
<p:selectOneRadio id="prior" value="#{fileMB.info.priority}">
<f:selectItem itemLabel="Normal" itemValue="Normal" />
<f:selectItem itemLabel="High" itemValue="High" />
<f:selectItem itemLabel="Medium" itemValue="Medium" />
<f:selectItem itemLabel="Low" itemValue="Low" />
</p:selectOneRadio>
<h:outputText for="Description" value="#{bundle.briefDescription}" />
<p:inputTextarea id="Description" value="#{fileMB.info.description}" rows="5" cols="30" counter="display" maxlength="10" counterTemplate="{0} characters remaining." autoResize="false" />
<h:outputText id="display" />
<br></br>
<p:commandButton value="Add Comment" icon="ui-icon-plus"
onclick="CreateComentDialogWidget.show();"/>
<br></br>
<br></br>
<p:commandButton value="create" actionListener="#{fileMB.save}" />
<p:commandButton value="cancel" icon="ui-icon-cancel"
onclick="CreateFileDialogWidget.hide();" type="button" />
</h:panelGrid>
</p:dialog>
</h:form>
<ui:include src="/pages/protected/filling/File/CreateComentDialog.xhtml" />
</h:body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<h:form id="CreateComentDialogForm" prependId="false">
<p:dialog widgetVar="CreateComentDialogWidget"
id="CreateComentDialogId" height="500" width="500" modal="true"
closable="true" draggable="true" resizable="false" header="Create File">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="coment" value="write coment"/>
<p:inputTextarea id="coment" value="#{notingMB.note.comment}" rows="5" cols="30" counter="display2" maxlength="50" counterTemplate="{0} characters remaining." autoResize="false" />
<h:outputText id="display2" />
<br></br>
<p:commandButton value="create" actionListener="#{notingMB.save}" />
<p:commandButton value="cancel" icon="ui-icon-cancel"
onclick="CreateComentDialogWidget.hide();" type="button" />
</h:panelGrid>
</p:dialog>
</h:form>
</h:body>
</html>
actionListener属性将接受javax.faces.event.ActionEvent参数 你应该改变
public void save() {
System.out.println("before save");
System.out.println(note.getComment());
controller.saveComment(note);
System.out.println("after save");
}
到
package com.managedBean.Filling;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.controller.Controller;
import com.managedBean.BaseMB;
import com.model.filling.Noting;
@ManagedBean(name="notingMB")
@SessionScoped
public class NotingMB extends BaseMB implements Serializable
{
public static final long serialVersionUID = 1L;
public static final Logger log = LogManager.getLogger(NotingMB.class.getName());
private Noting note;
public Noting getNote() {
return note;
}
public void setNote(Noting note) {
this.note = note;
}
public NotingMB()
{
this.note = new Noting();
//controller = Controller.getInstance();
}
public void save()
{
System.out.println("before save");
System.out.println(note.getComment());
controller.saveComment(note);
System.out.println("after save");
}
}
public void save() {
System.out.println("before save");
System.out.println(note.getComment());
controller.saveComment(note);
System.out.println("after save");
}
public void save(ActionEvent event) {
System.out.println("before save");
System.out.println(note.getComment());
controller.saveComment(note);
System.out.println("after save");
}