gpt4 book ai didi

java - 传递参数时获取 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 11:23:12 24 4
gpt4 key购买 nike

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

com.ri.pac.dao.FieldMappingDao.postFields 处的 java.lang.NullPointerException(FieldMappingDao.java:39)在 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。对于 null 检查,请添加以下代码:

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

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

关于java - 传递参数时获取 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49776543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com