写网上商城作业java.lang.NullPointerException
  1. 报错的:HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
com.eshore.action.GoodServlet.doPost(GoodServlet.java:56)
com.eshore.action.GoodServlet.doGet(GoodServlet.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.eshore.filters.EncodeFilter.doFilter(EncodeFilter.java:45)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.30 logs.


Apache Tomcat/7.0.30

2.goodservlet代码
package com.eshore.action;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eshore.factory.DAOFactory;
import com.eshore.pojo.Goods;

/**

  • 商品的Servlet类
    */
    @WebServlet(
    urlPatterns = { "/goods" },
    name = "goodsServlet"
    )
    public class GoodServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //判断action类型
    String action=request.getParameter("action");
    String path=null;
    Vector gVector=new Vector();

    try{
        if(action.equals("index-select")){//查询商品列表
            String keyWord=request.getParameter("keyWord");//获取查询的输入值
            String keyClass=request.getParameter("keyClass");//获取查询类别
            gVector=DAOFactory.getGoodDAOInstance().//获得所有商品
                queryAll(keyWord, keyClass);
            request.setAttribute("goods", gVector);
            path="goods/goodslist.jsp";
        }else if(action.equals("goodslist-select")){//指定商品列表
            Goods good=new Goods();
            String sid=request.getParameter("sid");//获得商品的id
            gVector=DAOFactory.getGoodDAOInstance().//获得指定的商品对象
                queryGoodBySid(Integer.valueOf(sid));
            if(gVector.size()>0&&gVector!=null)
                good =(Goods)gVector.get(0); 
            request.setAttribute("good", good);
            path="goods/good.jsp";
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    request.getRequestDispatcher(path).forward(request, response);
    

    }

}

4个回答

调试下,从错误信息看,是有变量为null,比如request.getParameter("xxx")这里有没有获得参数

你看下表里面有字段为非空,前面是空的,用debug看看,那个值为null了,或者开日志文件看下运行信息,就知道了

空指针异常,看页面与servlet之间传值的 名称 是否一致!

最底层的异常信息就是GoodServlet.java:56)这个类的56行,看看对象.操作有什么对象可能为空。
说时候,Java的空指针异常是最好解决的的了。自己调试下吧,少年。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问