我打算致电getParameter
。一切都很好,但当我点击编辑链接时,它会给出NullPointerException
。为什么它没有得到价值?
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 :(得分:2)
您收到空指针异常,因为单击编辑按钮时未传递delete
和show
的值。因此,del
和req
为空,您在req.equals("ok")
上收到例外。