DispatchAction的空指针异常

时间:2014-11-17 18:26:51

标签: list jsp nullpointerexception jstl struts

我正在尝试从UI中将记录插入MySQL,但我在JSP页面中获得了nullpointerexception。以下是我的文件。

Struts文件

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>

<action-mappings>        
        <action
            path="/inputemployee"
            forward="/pages/inputemployee.jsp"/> 

         <action  path="/db" parameter="method" type="com.pramesh.strutsdatabase.action.DataBaseAction"> 
            <forward name="success" path="/pages/insertsucess.jsp"/>
        </action>   
</action-mappings>

</struts-config>

inputemployee.jsp文件

    <%@taglib uri="http://struts.apache.org/tags-html"  prefix="html"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<script >
  function calculate(){
      document.frm.action="db.html";
      var selection=document.frm.sel.selectedIndex 

      if(selection==1){
      document.frm.method.value="insert";
      }
      document.frm.submit();  
  }
</script>

<body>

<form name ="frm">
 Enter Employee ID :<input name ="Employee ID"/>
 Enter Employee First Name :<input name ="First Name"/>
 Enter Employee Last Name :<input name ="Last Name"/>
 <select name ="sel">
      <option>Please Select</option>
      <option value="1">Insert</option>


</select> 
<input name ="method" type ="hidden" />

<input type ="button" value="Insert" onClick="calculate()"/>
</form>

</body>

</html>

Employee.java

package com.sigma.pojo;

import org.apache.struts.action.ActionForm;

公共类员工{

private int empId;
private String firstName;
private String lastName;

public int getEmpId() {
    return empId;
}
public void setEmpId(int empId) {
    this.empId = empId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
@Override
public String toString() {
    return "Employee [empId=" + empId + ", firstName=" + firstName
            + ", lastName=" + lastName + "]";
}

}

EmployeeDAO.java

package com.sigma.employeedDAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sigma.pojo.Employee;

public class EmployeeDAO {

    private Connection con = null;
    private java.sql.Statement stmt = null;
    private java.sql.PreparedStatement pstmt = null;
    private ResultSet rs = null;
    private Employee e;
    private List <Employee> list= null;

    private Connection getConnection(){
        if (con==null){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "kalokando11");
        } catch (ClassNotFoundException | SQLException e) {}
      }
        return con; 
    }

public List<Employee> getEmployee(){

         list = new ArrayList<Employee>();
         try {
                stmt = getConnection().createStatement();
                rs = stmt.executeQuery("select * from employee");

                    while(rs.next()){
                    frm = new Employee();
                    frm.setEmpId(rs.getInt("emp_id"));
                    frm.setFirstName(rs.getString("first_name"));
                    frm.setLastName(rs.getString("last_name"));
                    list.add(frm);  
                }               
            } catch (SQLException e1) { 
              }
         close(); 
         return list;
    }
    public void insert(Employee e1) throws SQLException{
        pstmt = getConnection().prepareStatement("Insert into employee values (?,?,?) ");
        pstmt.setInt(1,e1.getEmpId());
        pstmt.setString(2, e1.getFirstName());
        pstmt.setString(3,e1.getLastName());
        pstmt.execute();
        close();
    }

    public void close(){
        try {
            con.close();    
            stmt.close();
            pstmt.close();
            rs.close();
        } catch (SQLException e) {  
      }
    }
}

DataBaseAction.java

package com.pramesh.strutsdatabase.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sigma.employeedDAO.EmployeeDAO;
import com.sigma.pojo.Employee;

public class DataBaseAction extends DispatchAction{

public ActionForward display(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            response.setContentType("text/html");
            list =dao.getEmployee();  
            request.setAttribute("employee", list);  
            return mapping.findForward("success");
    }
    public ActionForward insert(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       Employee e1 = new Employee();

       response.setContentType("text/html");
       e1.setEmpId(Integer.parseInt(request.getParameter("Employee ID")));
       e1.setFirstName(request.getParameter("First Name"));
       e1.setLastName(request.getParameter("Last Name"));
       EmployeeDAO dao = new EmployeeDAO();
       dao.insert(e1);
       return mapping.findForward("success");
    }

}

insertsucess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Employee Added   
</body>
</html>

listemployee.jsp

<%@page import="java.util.List"%>
<%@page import="com.sigma.pojo.Employee"%>
<%@page import="com.pramesh.strutsdatabase.action.DataBaseAction"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body>
<table border=1>
        <thead>
            <tr>
                <th>Employee ID</th>
                <th>First Name</th>
                <th>Last Name</th>

                <th colspan=2>Action</th>
            </tr>
        </thead>
        <tbody>

            <c:forEach items="${employee}" var="user">
                <tr>
                    <td><c:out value="${user.empId}" /></td>
                    <td><c:out value="${user.firstName}" /></td>
                    <td><c:out value="${user.lastName}" /></td>               
                 </tr>
            </c:forEach>
        </tbody>
    </table>
</body>
</html>

当我调用EmployeeDAO insert 方法时,我得到 NullPointerException 。有人可以解释原因吗?我可以连接到数据库并更新表,但由于NullPointerExection,我无法显示insertsucess页面。

0 个答案:

没有答案