关于空指针异常,求助!!!
servlet代码如下:
[code=Java][/package com.experiment.usermanger;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.experiment.Dao.UserManagerDao;
import com.experiment.Dao.DapImpl.UserManagerDaoImpl;
import com.experiment.vo.UserInfo;
/**
* 对用户进行管理,调用Dao的增删改查,并对页面进行跳转。
*/
public class UserManagerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UserManagerServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("do get.........");
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 增加用户操作
String action = request.getParameter("action");
System.out.println(action);
if ("add".equals(action)) {
addUser(request,response);
}
if ("update".equals(action)){
updateUser(request,response);
}
if ("delete".equals(action)){
deleteUser(request,response);
}
}
private void deleteUser(HttpServletRequest request,
HttpServletResponse response) throws IOException {
System.out.println("删除数据!");
int deleteId = Integer.parseInt(request.getParameter("deleteid"));
UserManagerDao umd = new UserManagerDaoImpl();
umd.deleteById(deleteId);
response.sendRedirect("index.jsp");
}
private String parseBirthday(HttpServletRequest request,
HttpServletResponse response) {
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
String date = String.valueOf(year + "-" + month + "-" + day);
return date;
}
private void addUser(HttpServletRequest request,
HttpServletResponse response) throws IOException {
System.out.println("提交数据bbb");
// 进行添加操作
UserInfo useradd = new UserInfo();
String userAccount = request.getParameter("userId");
String userName = request.getParameter("nameAll");
String birthday = parseBirthday(request, response);
String sex = request.getParameter("sex");
System.out.println(userAccount+" "+userName+" "+birthday+" "+sex);
int sexnum = 0;
if ("男".equals(sex)) {
sexnum = 0;
} else {
sexnum = 1;
}
useradd.setAccount(userAccount);
useradd.setName(userName);
useradd.setBirthday(birthday);
useradd.setSex(sexnum);
UserManagerDao umd = new UserManagerDaoImpl();
umd.insert(useradd);
System.out.println("存储用户" + userName);
response.sendRedirect("index.jsp");
}
private void updateUser(HttpServletRequest request,
HttpServletResponse response) throws IOException {
System.out.println("更新用户");
// 进行添加操作
UserInfo userUpdate = new UserInfo();
int userId = Integer.parseInt(request.getParameter("userid"));
String userAccount = request.getParameter("updateAccount");
String userName = request.getParameter("updateName");
String birthday = request.getParameter("updateBirthday");
String sex = request.getParameter("sex");
int sexnum = 0;
if ("男".equals(sex)) {
sexnum = 0;
} else {
sexnum = 1;
}
userUpdate.setId(userId);
userUpdate.setAccount(userAccount);
userUpdate.setName(userName);
userUpdate.setBirthday(birthday);
userUpdate.setSex(sexnum);
UserManagerDao umd = new UserManagerDaoImpl();
umd.updateById(userUpdate);
System.out.println("更新id为"+ userId+"的用户" );
response.sendRedirect("index.jsp");
}
}]
jsp页面
[code=Java][/<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="com.experiment.vo.UserInfo"%>
<jsp:useBean id="db"
class="com.experiment.Dao.DapImpl.UserManagerDaoImpl" scope="request" />
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String account = request.getParameter("queryaccount");
UserInfo queryUser = db.queryByAccount(account);
String name = queryUser.getName();
String birthday = queryUser.getBirthday();
String sex = queryUser.getSex()==0?"男":"女";
int id = queryUser.getId();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
查询结果
<br>
<table border="1">
<tr>
<th>
账户
</th>
<th>
姓名
</th>
<th>
生日
</th>
<th>
性别
</th>
<th>
修改
</th>
<th>
删除
</th>
</tr>
<tr>
<td><%=account%></td>
<td><%=name%></td>
<td><%=birthday%></td>
<td><%=sex%></td>
<td><a href="update.jsp?id=<%=id%>">update</a></td>
<td><a href="UserManagerServlet?deleteid=<%=id%>&action=delete">delete</a></td>
</tr>
</table>
<br>
<form action="myForm.html">
<input type="submit" value="添加user"/>
</form>
<br>
<form action="showresult.jsp">
<input type="text" name="queryaccount" value="请输入查询账户">
<input type="submit" value="按账户查询">
</form>
</body>
</html>]
错误如下:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:491)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
org.apache.jsp.showresult_jsp._jspService(showresult_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.24 logs.
--------------------------------------------
Apache Tomcat/6.0.24
请问哪里错误,大家帮帮忙。。。。谢谢。。。。
[解决办法]
查一下\tomcat\work\Catalina\localhost\你的项目目录中找
showresult_jsp.java
查看76行,看看是什么
showresult_jsp.java:76
[解决办法]
showresult_jsp.java:76行是String name = queryUser.getName();
说明是queryUser为空,所以queryUser.getName();才会报空指针。
如果你要说String account = request.getParameter("queryaccount");一定会空指针,那么这个得根据业务来改,如果account 为空,你要怎么处理?给它一个默认值还是直接路过它的处理?
[解决办法]
直接跳过它的处理,也就是判断直接返回,if(account==null) return;
[解决办法]
showresult_jsp.java:76行是String name = queryUser.getName();
肯定是没做非空判断
说明是queryUser为空,所以queryUser.getName();才会报空指针。
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html