我在fieldMappingDao.postFields(theFields)中得到空指针异常; 你能帮我解决一下这个问题吗?
com.ri.pac.dao.FieldMappingDao.postFields(FieldMappingDao.java:39)中的java.lang.NullPointerException 在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);
}
}
答案 0 :(得分:1)
虽然问题尚未完成,但在 doPost 方法中,变量 fieldLabel 和 displayColumn 的值可能为null。对于null检查,请添加以下代码:
if(fieldLabel == null) fieldLabel = "";
if(displayColumn == null) displayColumn = "";