传递参数时获取java.lang.NullPointerException [复制](Getting java.lang.NullPointerException while passing argu

编程入门 行业动态 更新时间:2024-10-28 12:22:44
传递参数时获取java.lang.NullPointerException [复制](Getting java.lang.NullPointerException while passing arguments [duplicate])

这个问题在这里已经有了答案:

什么是NullPointerException,以及如何解决它? 12个答案

我在fieldMappingDao.postFields(theFields)中获得空指针异常; 你能帮我解决这个问题吗?

java.lang.NullPointerException at com.ri.pac.dao.FieldMappingDao.postFields(FieldMappingDao.java:39)at com.ri.pac.servlet.PostFieldsMappingServlet.doPost(PostFieldsMappingServlet.java:127)

package com.ri.pac.dao; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; public class FieldMappingDao { private DataSource dataSource; public FieldMappingDao(DataSource theDataSource) { dataSource = theDataSource; } public void postFields(FieldsMapping theFields) throws Exception { Connection myConn = null; PreparedStatement myStmt = null; try { // create sql for insert String sql = "INSERT INTO FIELDS" + "( Field_Label,Version,Display_Column,Display_Order," + "Creation_Date)" + "values (?,?,?,?,?)"; myStmt = myConn.prepareStatement(sql); myStmt.setString(1, theFields.getFieldLabel()); myStmt.setInt(2, theFields.getVersion()); myStmt.setString(3, theFields.getDisplayColumn()); myStmt.setInt(4, theFields.getDisplayOrder()); Date sqlCreationDate= new Date( theFields.getCreationDate().getTime()); myStmt.setDate(5,sqlCreationDate); myStmt.execute(); } finally { // clean up JDBC objects close(myConn, myStmt, null); } } private void close(Connection myConn, Statement myStmt, ResultSet myRs) { try { if (myRs != null) { myRs.close(); } if (myStmt != null) { myStmt.close(); } if (myConn != null) { myConn.close(); } } catch (Exception exc) { exc.printStackTrace(); } } } package com.ri.pac.servlet; import java.io.IOException; @WebServlet("/postFieldMappings") public class PostFieldsMappingServlet extends HttpServlet { private static final long serialVersionUID = 1L; private FieldMappingDao fieldMappingDao ; @Resource(name="jdbc/Person") private DataSource dataSource; @Override public void init() throws ServletException { super.init(); try { fieldMappingDao= new FieldMappingDao(dataSource); } catch (Exception exc) { throw new ServletException(exc); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { List<MatchType> matchTypes =fieldMappingDao.getMatchTypeId(); request.setAttribute("Type",matchTypes); } catch (Exception e) { e.printStackTrace(); } RequestDispatcher rdispatcher=request.getRequestDispatcher("/add-fields.jsp"); rdispatcher.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fieldLabel=request.getParameter("fieldLabel"); int version=Integer.parseInt(request.getParameter("version")); String displayColumn=request.getParameter("displayColumn"); int displayOrder=Integer.valueOf(request.getParameter("displayOrder")); Date creationDate =new Date(); Date lastUpdateDate = new Date(); FieldsMapping theFields=new FieldsMapping(); theFields.setFieldLabel(fieldLabel); theFields.setVersion(version) theFields.setDisplayColumn(displayColumn); theFields.setDisplayOrder(displayOrder); theFields.setCreationDate(creationDate); try { fieldMappingDao.postFields(theFields); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } RequestDispatcher rdispatcher=request.getRequestDispatcher("/confirmation.jsp"); rdispatcher.forward(request, response); }

}

This question already has an answer here:

What is a NullPointerException, and how do I fix it? 12 answers

I am getting null pointer exception in fieldMappingDao.postFields(theFields); Could you please help me how can I solve this problem.

java.lang.NullPointerException at com.ri.pac.dao.FieldMappingDao.postFields(FieldMappingDao.java:39) at com.ri.pac.servlet.PostFieldsMappingServlet.doPost(PostFieldsMappingServlet.java:127)

package com.ri.pac.dao; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; public class FieldMappingDao { private DataSource dataSource; public FieldMappingDao(DataSource theDataSource) { dataSource = theDataSource; } public void postFields(FieldsMapping theFields) throws Exception { Connection myConn = null; PreparedStatement myStmt = null; try { // create sql for insert String sql = "INSERT INTO FIELDS" + "( Field_Label,Version,Display_Column,Display_Order," + "Creation_Date)" + "values (?,?,?,?,?)"; myStmt = myConn.prepareStatement(sql); myStmt.setString(1, theFields.getFieldLabel()); myStmt.setInt(2, theFields.getVersion()); myStmt.setString(3, theFields.getDisplayColumn()); myStmt.setInt(4, theFields.getDisplayOrder()); Date sqlCreationDate= new Date( theFields.getCreationDate().getTime()); myStmt.setDate(5,sqlCreationDate); myStmt.execute(); } finally { // clean up JDBC objects close(myConn, myStmt, null); } } private void close(Connection myConn, Statement myStmt, ResultSet myRs) { try { if (myRs != null) { myRs.close(); } if (myStmt != null) { myStmt.close(); } if (myConn != null) { myConn.close(); } } catch (Exception exc) { exc.printStackTrace(); } } } package com.ri.pac.servlet; import java.io.IOException; @WebServlet("/postFieldMappings") public class PostFieldsMappingServlet extends HttpServlet { private static final long serialVersionUID = 1L; private FieldMappingDao fieldMappingDao ; @Resource(name="jdbc/Person") private DataSource dataSource; @Override public void init() throws ServletException { super.init(); try { fieldMappingDao= new FieldMappingDao(dataSource); } catch (Exception exc) { throw new ServletException(exc); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { List<MatchType> matchTypes =fieldMappingDao.getMatchTypeId(); request.setAttribute("Type",matchTypes); } catch (Exception e) { e.printStackTrace(); } RequestDispatcher rdispatcher=request.getRequestDispatcher("/add-fields.jsp"); rdispatcher.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fieldLabel=request.getParameter("fieldLabel"); int version=Integer.parseInt(request.getParameter("version")); String displayColumn=request.getParameter("displayColumn"); int displayOrder=Integer.valueOf(request.getParameter("displayOrder")); Date creationDate =new Date(); Date lastUpdateDate = new Date(); FieldsMapping theFields=new FieldsMapping(); theFields.setFieldLabel(fieldLabel); theFields.setVersion(version) theFields.setDisplayColumn(displayColumn); theFields.setDisplayOrder(displayOrder); theFields.setCreationDate(creationDate); try { fieldMappingDao.postFields(theFields); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } RequestDispatcher rdispatcher=request.getRequestDispatcher("/confirmation.jsp"); rdispatcher.forward(request, response); }

}

最满意答案

虽然问题不完整,但在doPost方法中,变量fieldLabeldisplayColumn的值可能为null。 对于空检查,请添加以下代码:

if(fieldLabel == null) fieldLabel = ""; if(displayColumn == null) displayColumn = "";

Although the question is not complete, In the doPost method the value of the variable fieldLabel and displayColumn may null. For null check please add the following code:

if(fieldLabel == null) fieldLabel = ""; if(displayColumn == null) displayColumn = "";

更多推荐

本文发布于:2023-07-26 16:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   lang   java   NullPointerException   arguments

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!