0

I have primefaces page, where displayed my data from mysql. table structure:

+---------------------------+---------------+---------+
| id (PK, AUTO_INCREMENT)   | name(Varchar) | age(int)|
+---------------------------+---------------+---------+

http://imageshack.com/a/img538/9755/2PSVyf.png

Button add well working, but when I want to delete person or update, I get errors like this

апр 29, 2015 6:28:21 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
        WARNING: /pages/test.xhtml @40,84 listener="#{test.update()}": java.lang.NullPointerException
        javax.el.ELException: /pages/test.xhtml @40,84 listener="#{test.update()}": java.lang.NullPointerException
            at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
            at org.primefaces.behavior.ajax.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxBehaviorListenerImpl.java:54)
            at org.primefaces.event.RowEditEvent.processListener(RowEditEvent.java:41)
            at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106)
            at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:760)
            at javax.faces.component.UIData.broadcast(UIData.java:1071)
            at javax.faces.component.UIData.broadcast(UIData.java:1093)
            at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
            at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
            at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
            at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
            at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
            at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
            at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
            at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
            at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
            at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            at java.lang.Thread.run(Thread.java:745)
        Caused by: java.lang.NullPointerException
            at kz.mtf.dao.TestDAO.update(TestDAO.java:46)
            at kz.mtf.controller.TestController.update(TestController.java:42)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:606)
            at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
            at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
            at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
            ... 35 more

DAO:

public class TestDAO {
    public static List<TestModel> getAll() throws SQLException {
        List<TestModel> list = new ArrayList<TestModel>();
        Connection con = Database.getConnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM a_test");
        while ( rs.next() ) {
            TestModel testModel = new TestModel();
            testModel.setName(rs.getString("name"));
            testModel.setAge(rs.getInt("age"));
            testModel.setId(rs.getLong("id"));

            list.add(testModel);
        }
        return list;
    }

    public static void insert(TestModel testModel) throws SQLException{
        Connection con = Database.getConnection();
        PreparedStatement ps = con.prepareStatement("INSERT INTO a_test (name, age) VALUES (?, ?)");

        ps.setString(1,testModel.getName());
        ps.setInt(2, testModel.getAge());
        ps.executeUpdate();

    }

    public static void update(TestModel testModel) throws SQLException{
        Connection con = Database.getConnection();
        PreparedStatement ps = con.prepareStatement("update a_test set name=?, age=?, where id=?");

        ps.setString(1,testModel.getName());
        ps.setInt(2, testModel.getAge());
        ps.setLong(3, testModel.getId());
        ps.executeUpdate();

    }

    public static void delete(long id) throws SQLException{
        Connection con = Database.getConnection();
        PreparedStatement ps = con.prepareStatement("delete from a_test where id=?");

        ps.setLong(1, id);
        ps.executeUpdate();

    }
}

Controller:

@ManagedBean(name="test")
@RequestScoped
public class TestController implements Serializable{
    private TestModel testModel;
    private List<TestModel> testModelList;

    public TestController() {
        testModel = new TestModel();
    }

    public TestModel getTestModel() {
        return testModel;
    }

    public void setTestModel(TestModel testModel) {
        this.testModel = testModel;
    }

    public List<TestModel> getTestModelList() {
        return testModelList;
    }

    public void setTestModelList(List<TestModel> testModelList) {
        this.testModelList = testModelList;
    }

    public void update() throws SQLException {
        TestDAO.update(testModel);
    }

    public void insert() throws SQLException {
        TestDAO.insert(testModel);
    }

    public List<TestModel> getAll() throws SQLException {
        return TestDAO.getAll();
    }

    public void delete() throws SQLException {
        TestDAO.delete(testModel.getId());
    }
}

test.xhtml:

 <h:form id="form1">
    <p:panel id="panel" header="Add new person" style="margin-bottom:10px;">
        <p:messages id="messages" />
        <h:panelGrid columns="2" cellpadding="5">
            <p:outputLabel  for="a1" value="Name: " />
            <p:inputText  id="a1" value="#{test.testModel.name}"/>

            <p:outputLabel for="a2" value="Age: " />
            <p:inputText id="a2" value="#{test.testModel.age}"/>
        </h:panelGrid>
    </p:panel>

    <p:toolbar>
        <f:facet name="left">
            <p:commandButton value="Add"  update="form1" action="#{test.insert()}"/>
        </f:facet>
    </p:toolbar>

    <p:spacer height="30px;"/>

    <p:dataTable value="#{test.getAll()}" var="e"  widgetVar="50" editable="true" >
        <f:facet name="header">
           Persons
        </f:facet>

        <p:ajax event="rowEdit" listener="#{test.update()}" update=":form1:messages" />
        <p:ajax event="rowEditCancel" listener="#{test.delete()}" update=":form1:messages" />

        <p:column>
            <f:facet name="header">
                <h:outputText value="Name" />
            </f:facet>
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{e.name}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{e.name}" style="width:100%"/>
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column>
            <f:facet name="header">
                <h:outputText value="Age" />
            </f:facet>
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{e.age}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{e.age}" style="width:100%"/>
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="" style="width:50px">
            <p:rowEditor />
        </p:column>
    </p:dataTable>
    </h:form>

How can I fix it?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • PrimeFaces does not update anything anywhere in a database. Your code does that (or the container or... ) So this is not PrimeFaces related (test.update() is your code) – Kukeltje Apr 29 '15 at 14:19
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Kukeltje Apr 29 '15 at 14:20
  • i know that null pointer exception, i asked why is null? and what i can do to fix? – ALisher Kozhabaev Apr 29 '15 at 15:28
  • Did you check the scope of DAO bean? – Phani May 01 '15 at 14:52

0 Answers0