每次我点击指向其他页面的链接时,都会出现Null Pointer Exception错误,我不知道是什么问题。
这是错误
05-Jul-2014 17:27:25.941 SEVERE [http-nio-8084-exec-14] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [itemServlet] in context with path [/servlet] threw exception
java.lang.NullPointerException
at com.etc.servlet.itemServlet.showProductList(itemServlet.java:172)
at com.etc.servlet.itemServlet.doPost(itemServlet.java:94)
at com.etc.servlet.itemServlet.doGet(itemServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这是抛出NullPointerException
的servlet的部分代码public class itemServlet extends HttpServlet {
private memberDao usrdao = new memberDao();
private itemDao itmdao = new itemDao();
private noticeDao ntcdao=new noticeDao();
private articleDao atcdao=new articleDao();
private List<item> listItm;
private List<notice> listNtc;
private List<article> listAtc;
private List<member> listUsr;
private List<item> listpiece;
/**
* Constructor of the object.
*/
public itemServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1 中文处理
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");
//2 读取参数op
String op = request.getParameter("op");
//3 根据op调用不同的业务方法
if("add".equals(op)){
additem(request,response);//
}else if("findall".equals(op)){
showProductList(request,response);//展现所有商品
}else if("finddetail".equals(op)){
showProductDetail(request,response);//展现商品的细节
}else if("addcart".equals(op)){
addcart(request,response);//添加购物车
}
}
private void addcart(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException//展示商品细节
{
int itmID=Integer.parseInt(request.getParameter("itemID"));
System.out.println("获取本页的商品编号"+itmID);
item additem = itmdao.findbyitmmID(itmID);
HttpSession session =request.getSession();
if(request.getSession().getAttribute("listpiece")==null){
listpiece=new ArrayList<item>();
listpiece.add(additem);
System.out.println("listpiece为空。将查到的商品作为单件加入listpiece的列表");
session.setAttribute("listpiece", listpiece);
}
else{
List<item> listpiece=(List<item>) request.getSession().getAttribute("listpiece");
listpiece.add(additem);
System.out.println("listpiece不为空,将查到的商品作为单件加入listpiece的列表");
session.setAttribute("listpiece", listpiece);
}
if(listItm==null)
{
response.getWriter().print("查询失败!");
return;
}
int itid = Integer.parseInt(request.getParameter("itemID"));
for(item it:listItm)
{
if(itid==it.getItemID())//找出该对象
{
request.setAttribute("it", it);
request.getRequestDispatcher("../itemdetail.jsp").forward(request, response);
return;
}
}
}
private void showProductDetail(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException//展示商品细节
{
if(listItm==null)
{
response.getWriter().print("查询失败!");
return;
}
int itid = Integer.parseInt(request.getParameter("itemID"));
for(item it:listItm)
{
if(itid==it.getItemID())//找出该对象
{
request.setAttribute("it", it);
request.getRequestDispatcher("../itemdetail.jsp").forward(request, response);
return;
}
}
}
private void showProductList(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException//商品成列
{
listItm = itmdao.findAll();
listNtc = ntcdao.findSix();
listAtc = atcdao.findTen();
request.setAttribute("listNtc", listNtc);
request.setAttribute("listAtc", listAtc);
request.setAttribute("listItm", listItm);
request.getRequestDispatcher("../showlist.jsp").forward(request, response);
}
private void additem(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException//添加一件新的商品
{
//1 图片上传
SmartUpload sm = new SmartUpload();
sm.initialize(this.getServletConfig(), request, response);
sm.setAllowedFilesList("jpg,jpeg,png,gif");
try
{
sm.upload();
sm.save("/pic");//在pic路径下按原来的名字进行保存
} catch (SmartUploadException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
response.getWriter().print("上传失败!添加失败!");
return;
}
//2 读取表单的其他数据,插入数据库
String itemName = sm.getRequest().getParameter("itemName");
float itemPrice = Float.parseFloat(sm.getRequest().getParameter("price"));
float itemPrice = Float.parseFloat(sm.getRequest().getParameter("itemPrice"));
String itemStatus = sm.getRequest().getParameter("itemStatus");
String itemBrief = sm.getRequest().getParameter("itemBrief");
int typeID =Integer.parseInt(sm.getRequest().getParameter("typeID"));
item newitem = new item(-1,itemName,itemPrice,itemStatus,itemBrief,typeID );
if (!itmdao.add(newitem))
{
response.getWriter().print("添加失败!");
return;
}
request.getRequestDispatcher("itemServlet?op=findall").forward(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
空指针是否可能来自数据库?
itmDao.findall()
public List<item> findAll()
{
System.out.println("itemDao的findAll被调用");
List<item> list = new ArrayList();
ResultSet rs = DBUtils.doQuery("select * from itemTable order by itemID desc");
try {
if (rs == null)
return list;
while (rs.next()) {
item item = new item(rs.getInt("itemID"),
rs.getString("itemName"), rs.getFloat("itemPrice"),rs.getString("itemStatus"),rs.getString("itembrief")
,rs.getInt("typeID"));
list.add(item);
}
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
DBUtils.close(rs, rs.getStatement(), rs.getStatement()
.getConnection());
} catch (Exception e2)
{
e2.printStackTrace();
}
}
return list;
}
ntddao.findsix()
public List<notice> findSix() //用在首页的 查找前六条的公告
{
System.out.println("noticeDao的findSix函数被调用");
List<notice> list = new ArrayList();
ResultSet rs = DBUtils.doQuery("select * from notice order by ntID desc");
try {
if (rs == null)
return list;
int i=1;
while (rs.next()&&i<=6) {
notice notice = new notice(rs.getInt("ntID"),
rs.getString("ntTitle"), rs.getString("ntTime"),rs.getString("ntText"),rs.getInt("adID"));
list.add(notice);
i++;
}
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
DBUtils.close(rs, rs.getStatement(), rs.getStatement()
.getConnection());
} catch (Exception e2)
{
e2.printStackTrace();
}
}
return list;
}
atcdao.findten()
public List<article> findTen() //用在首页的 查找前10条的公告
{
System.out.println("articleDao的findTen函数被调用");
List<article> list = new ArrayList();
ResultSet rs = DBUtils.doQuery("select * from article order by atID desc");
try {
if (rs == null)
return list;
int i=1;
while (rs.next()&&i<=10) {
article article = new article(rs.getInt("atID"),
rs.getString("atTitle"), rs.getString("atPic"),rs.getString("atTime"),rs.getString("atText"),rs.getInt("adID"));
list.add(article);
i++;
}
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
DBUtils.close(rs, rs.getStatement(), rs.getStatement()
.getConnection());
} catch (Exception e2)
{
e2.printStackTrace();
}
}
return list;
}
答案 0 :(得分:2)
您提供给getRequestDispatcher
的路径必须位于当前的servlet上下文中。这意味着你不能使用像"../showlist.jsp"
这样的东西。您将不得不以不同方式组织目录结构。
答案 1 :(得分:0)
你可以在那里检查以下对象是否为空?
listItm = itmdao.findAll();
listNtc = ntcdao.findSix();
listAtc = atcdao.findTen();
我假设其中一个(或多个)为null,因此setAttribute会抛出异常。
否则我强烈建议不要强制使用在内存中作为单例运行的servlet中的字段,并且多个用户请求可能会破坏数据的一致性。 我在谈论这些领域:
private List<item> listItm;
private List<notice> listNtc;
private List<article> listAtc;
private List<member> listUsr;
private List<item> listpiece;
我的另一个建议是遵循java命名约定。例如,将类的第一个字符更改为capital:itemServlet - &gt; ItemServlet。
这是更标准的方式,有助于我们更好地理解您的代码:)