你好我正在使用mvc架构开发一个web-app我试图通过服务层将数据从表单插入到数据库但它抛出一个空指针异常:
以下是我的Servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Affiliate af= new Affiliate();
af.setFisrtName(request.getParameter("txtFname"));
af.setLastName(request.getParameter("txtLname"));
af.setGender(request.getParameter("txtGender"));
af.setCategory(request.getParameter("txtCategory"));
String dob=(request.getParameter("txtDob"));
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date;
try {
date = (Date)formatter.parse(dob);
af.setDate(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
af.setAge(Integer.parseInt(request.getParameter("txtAge")));
af.setAddress(request.getParameter("txtAddr"));
af.setCountry("India");
af.setState(request.getParameter("txtState"));
af.setCity(request.getParameter("txtCity"));
af.setPinCode(Integer.parseInt(request.getParameter("txtPin")));
af.setEmailId(request.getParameter("txtEmail"));
af.setStd(Integer.parseInt(request.getParameter("txtStd")));
af.setContactNo(Integer.parseInt(request.getParameter("txtPhone")));
af.setMobileNo(Long.parseLong(request.getParameter("txtMobile"),10));
AffiliateService afs=new AffiliateService();
**afs.createAffiliate(af);**
}
}
我的服务代码是:
public class AffiliateService {
Affiliate affiliate=null;
public Affiliate createAffiliate( Affiliate affiliate) {
**validateAffiliate(affiliate);**
return affiliate;
}
private Affiliate validateAffiliate(Affiliate affiliate) {
this.affiliate=affiliate;
if(affiliate!=null){
AffiliateDAO afd=new AffiliateDAO();
**afd.insertAffiliate(affiliate);**
}
return affiliate;
}
}
我的DAO代码如下:
public class AffiliateDAO {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public List<Affiliate> addAffiliate(){
ArrayList<Affiliate> affiliates = new ArrayList<Affiliate>();
return affiliates;
}
public void updateAffiliate(Affiliate affiliate){
}
public void delteAffiliate(Affiliate affiliate){
}
public void selectAffiliate(Affiliate affiliate){
}
public void insertAffiliate(Affiliate affiliate){
String sql="INSERT INTO REGISTER " +"(id,FisrtName,LastName,Gender,Category,DateOfBirth,Age,Address,Country,State,City,PinCode,EmailId,Std,ContactNo,MobileNo)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Connection conn = null;
try {
**conn = dataSource.createConnection();**
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, affiliate.getId());
ps.setString(2, affiliate.getFisrtName());
ps.setString(3, affiliate.getLastName());
ps.setString(4,affiliate.getGender());
ps.setString(5, affiliate.getCategory());
ps.setDate(6, (Date) affiliate.getDate());
ps.setInt(7, affiliate.getAge());
ps.setString(8, affiliate.getAddress());
ps.setString(9,affiliate.getCountry());
ps.setString(10,affiliate.getState());
ps.setString(11, affiliate.getCity());
ps.setInt(12, affiliate.getPinCode());
ps.setString(13, affiliate.getEmailId());
ps.setInt(14,affiliate.getStd());
ps.setInt(15, affiliate.getContactNo());
ps.setLong(16, affiliate.getMobileNo());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
public Affiliate searchById(int id){
String sql = "SELECT * FROM REGISTER WHERE id = ?";
Connection conn = null;
try {
conn = dataSource.createConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
Affiliate affiliate = null;
ResultSet rs = ps.executeQuery();
if (rs.next()) {
rs.getInt("id");
rs.getString("FisrtName");
rs.getString("LastName");
rs.getString("Gender");
rs.getString("Category");
rs.getDate("DateOfBirth");
rs.getString("Age");
rs.getString("Address");
rs.getString("Country");
rs.getString("State");
rs.getString("City");
rs.getInt("PinCode");
rs.getString("EmailId");
rs.getInt("Std+ContactNo");
rs.getString("MobileNo");
}
rs.close();
ps.close();
return affiliate;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
}
这是我的dataSource类:
public class DataSource {
Connection connection=null;
BasicDataSource bdsource=new BasicDataSource();
public DataSource(){
bdsource.setUrl("dbUrl");
bdsource.setUsername("dbuserName");
bdsource.setPassword("dbPassword");
bdsource.setDriverClassName("com.mysql.jdbc.Driver");
}
public Connection createConnection(){
Connection con=null;
try{
if(connection !=null){
System.out.println("Can't create a new connection");
}
else{
con=bdsource.getConnection();
}
}
catch(Exception e){
e.printStackTrace();
}
return con;
}
}
我的堆栈跟踪如下:
java.lang.NullPointerException
com.affiliate.DAO.AffiliateDAO.insertAffiliate(AffiliateDAO.java:43)
com.affiliate.service.AffiliateService.validateAffiliate(AffiliateService.java:21)
com.affiliate.service.AffiliateService.createAffiliate(AffiliateService.java:12)
com.affiliate.servlet.AffiliateServlet.doPost(AffiliateServlet.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
com.affiliate.servlet.RegisterServlet.doPost(RegisterServlet.java:42)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
代码中的所有粗体线都与堆栈跟踪相对应。 请帮我解决这个问题。我怀疑我的验证方法......
答案 0 :(得分:3)
dataSource
似乎null
,因为永远不会调用setDataSource
。
答案 1 :(得分:2)
您需要修改validateAffiliate(Affiliate affiliate)
类的AffiliateService
方法。您从未初始化导致NPE 的数据源。
检查一下:
private Affiliate validateAffiliate(Affiliate affiliate) {
this.affiliate=affiliate;
if(affiliate!=null){
AffiliateDAO afd=new AffiliateDAO();
// This was causing NPE. Data source must be set before using it.
afd.setDataSource(passDataSourceInstance);
afd.insertAffiliate(affiliate);
}
答案 2 :(得分:0)
在调用conn = dataSource.createConnection()之前初始化dataSource; 可以在AffiliateDAO构造函数中。