我更新时会发生java.lang.NullPointerException吗?

时间:2019-01-20 14:52:31

标签: java nullpointerexception

我得到了空指针异常 "java.lang.NullPointerException" 请参考以下代码以供参考 一切都应该很好,但不能将数据更新到数据库(mySql)

<% try{
        //reading request parameters

          String projectName=request.getParameter("projectName").trim();
          String status=request.getParameter("status").trim();

          //String user = session.getAttribute("user").toString();
          Connection connection = DAO.getConnection();
          PreparedStatement pstmt = connection.prepareStatement("update project set status=? where projectName=?");

          pstmt.setString(1,status);
          pstmt.executeUpdate();                    
          out.println("<h2 style='color:green'>Project Status has been updated</h2>");
    }
    catch(Exception ex)
    {
          ex.printStackTrace();
          out.println("<h2 style='color:red'>Please login to update your Status</h2>");
    }
%>

由于异常而无法更新 参考下面 请任何人建议我如何解决此异常...

  org.apache.jsp.MyWorkPanelAction_jsp._jspService(MyWorkPanelAction_jsp.java:93)上的

java.lang.NullPointerException     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)在   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)     在   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:310)     在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)     在java.lang.Thread.run(未知来源)

1 个答案:

答案 0 :(得分:1)

您错过了projectName ...请尝试下面的代码

try{
        //reading request parameters

          String projectName=request.getParameter("projectName").trim();
          String status=request.getParameter("status").trim();

          //String user = session.getAttribute("user").toString();
          Connection connection = DAO.getConnection();
          PreparedStatement pstmt = connection.prepareStatement("update project set status=? where projectName=?");

          pstmt.setString(1,status);
          pstmt.setString(2,projectName); //Try this

          pstmt.executeUpdate();                    
          out.println("<h2 style='color:green'>Project Status has been updated</h2>");
    }
    catch(Exception ex)
    {
          ex.printStackTrace();
          out.println("<h2 style='color:red'>Please login to update your Status</h2>");
    }