JSF - 从数据表中删除时出现空指针异常

时间:2018-05-03 22:51:01

标签: java jsf java-ee nullpointerexception

我正在使用eclipse开发一个jsf应用程序,其中我有一个从数据库成功加载的数据表,我在托管bean中添加了一个删除按钮和一个删除方法,但问题是当我点击它那个按钮时,它给出一个空指针异常。 我该如何解决这个问题?

受管Bean

@ManagedBean
@SessionScoped
public class MonitorProjectsBean {

@EJB
private ProjectServiceLocal projectServiceLocal;
private List<ProjectMonitoring> listpm=new ArrayList<ProjectMonitoring>();
private ProjectMonitoring selectedPm;
private ProjectMonitoringServiceLocal projectMonitoringServiceLocal;
public String delete(ProjectMonitoring pm){
System.out.println("delte method");
        projectMonitoringServiceLocal.delete(pm);
    return "/pages/admin/MonitorProjects?faces-redirect=true";}
@PostConstruct
public void init() {
listpm=projectServiceLocal.findAllProjectMonitoring();
selectedPm =new ProjectMonitoring();
//System.out.println(listpm.get(0).getDescription());
}
public String addPM()
{
    projectMonitoringServiceLocal.save(selectedPm);
    return "/pages/admin/MonitorProjects?faces-redirect=true";
}
public ProjectServiceLocal getProjectServiceLocal() {
    return projectServiceLocal;
}

public void setProjectServiceLocal(ProjectServiceLocal projectServiceLocal) {
    this.projectServiceLocal = projectServiceLocal;
}

public List<ProjectMonitoring> getListpm() {
    return listpm;
}

public void setListpm(List<ProjectMonitoring> listpm) {
    this.listpm = listpm;
}

public ProjectMonitoring getSelectedPm() {
    return selectedPm;
}

public void setSelectedPm(ProjectMonitoring selectedPm) {
    this.selectedPm = selectedPm;
}
public ProjectMonitoringServiceLocal getProjectMonitoringServiceLocal() {
    return projectMonitoringServiceLocal;
}
public void setProjectMonitoringServiceLocal(ProjectMonitoringServiceLocal projectMonitoringServiceLocal) {
    this.projectMonitoringServiceLocal = projectMonitoringServiceLocal;}}

XHTML

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
template="/template/template.xhtml">

<ui:define name="pageContent">
    <h:head>

    </h:head>
    <h:body>
    <h3>Manage Projects monitoring</h3>

<h:form>

    <h:dataTable  value="#{monitorProjectsBean.listpm}" var="u" styleClass="hover" id="myTable">

        <p:column>
            <f:facet name="header">Date</f:facet>
            <p:outputLabel value="#{u.date}"></p:outputLabel>
        </p:column>
        <p:column>
            <f:facet name="header">First Delay</f:facet>
            <p:outputLabel value="#{u.delay}"></p:outputLabel>
        </p:column>
        <p:column>
            <f:facet name="header">Description</f:facet>
            <p:outputLabel value="#{u.description}"></p:outputLabel>
        </p:column>
        <p:column>
            <f:facet name="header">Remove</f:facet>
        <h:panelGroup>
        <h:commandButton  action="#{monitorProjectsBean.delete(u)}" value="remove" styleClass=""/>              
        </h:panelGroup>
        </p:column>
        <p:column>
            <f:facet name="header">Edit</f:facet>
            <h:commandButton action="#{editUser.load}" value="edit" styleClass="btn btn-primary">
                <f:setPropertyActionListener value="#{u}" target="#{editUser.u}"></f:setPropertyActionListener>
            </h:commandButton>          

        </p:column>

    </h:dataTable>
    </h:form>

</h:body>
</ui:define>
</ui:composition>

错误

Servlet Path:
/pages/admin/MonitorProjects.jsf

Path Info:
null

Query String:
null

Stack Trace
javax.servlet.ServletException: java.lang.NullPointerException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:667)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:1)

你的ProjectMonitoringServiceLocal是EJB吗?调用并在调用delete时检查此projectMonitoringServiceLocal是否为空。

看起来你错过了那里的注释:

@EJB private ProjectMonitoringServiceLocal projectMonitoringServiceLocal;