2015-04-21 68 views
0

我試圖撥打getParameter。一切都很好,但是當我點擊編輯鏈接時,它會給出一個NullPointerException。爲什麼它沒有得到價值?servlet中的getParameter

JSP代碼:

<%@page import="teacher.TeacherBAL"%> 
    <%@page import="teacher.TeacherBean" %> 
    <%@page import="java.util.*" %> 
    <%@ 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>Display Data</title> 
</head> 
<body> 
    <table border=1> 
     <th>Name</th> 
     <th>Degree</th> 
     <th>Delete</th> 
     <th>Edit</th> 
      <% 
       ArrayList teachers = (ArrayList) request.getAttribute("allRecord"); 
       if (teachers != null) { 
        int a = 0; 
        for (; a < teachers.size(); a++) { 
         TeacherBean teacherBea = (TeacherBean) teachers.get(a); 
      %> 
     <tr> 
      <td><%=teacherBea.getName()%></td> 
      <td><%=teacherBea.getDegree()%></td> 
      <td> 
       <a href="FetchData?delete=<%=a%>">Delete</a>   
      </td> 
      <td> 
       <a href="FetchData?click=<%=a%>">Edit</a> 
      </td> 
     </tr> 
     <%} 
      } 
     %> 
     </table> 
    <a href="index.jsp" ><img src="images/back.jpg" height="60"></a> 
</body> 

servlet代碼:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    String edit= request.getParameter("click"); 
    String del = request.getParameter("delete"); 
    String req= request.getParameter("show"); 
    if(del!=null){ 
     int dele = Integer.parseInt(del); 
     TeacherBAL.getTeacher().remove(dele); 
     request.setAttribute("allRecord", TeacherBAL.getTeacher()); 
     RequestDispatcher rd= request.getRequestDispatcher("display.jsp"); 
     rd.forward(request, response); 
    } 
    else if(req.equals("ok")){ 
     request.setAttribute("allRecord", TeacherBAL.getTeacher()); 
     RequestDispatcher rd= request.getRequestDispatcher("display.jsp"); 
     rd.forward(request, response); 
    } 
    else if(edit!=null){ 
     int num = Integer.parseInt(edit); 
     PrintWriter out = response.getWriter(); 
     out.print(num); 
    } 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String name= request.getParameter("names"); 
    String degre= request.getParameter("degre"); 
    if(name!=null && degre!=null){ 
     TeacherBAL.setTeacher(new TeacherBean(name,degre)); 
     RequestDispatcher rd= request.getRequestDispatcher("index.jsp"); 
     rd.forward(request, response); 
    } 
} 
+0

NullPointerException的堆棧跟蹤對於那些正在尋找您的問題的人非常有幫助。 –

回答

2

由於您單擊編輯按鈕時沒有通過deleteshow的值,因此您會收到空指針異常。因此,delreq爲空,您在req.equals("ok")上遇到異常。

+1

實際上,它只是用於''show「'參數,而不是''delete''參數。 –