0
 class mstLeastCountEdit {
    mstLeastCount reff;
    HttpServletRequest request;
    HttpServletResponse response;
    String msg = "";
    String LcDesc="";
    int i = 0;
    int noOfRows = 0;
    HttpSession session=request.getSession(true);
    Integer ccode=(Integer) session.getAttribute("companycode");

    void updateEdit(mstLeastCount reff,HttpServletRequest request, HttpServletResponse response, int k)
    throws ServletException,IOException,IllegalStateException {
        int j = k;
        this.reff=reff;
        this.request=request;
        this.response=response;
        try{
             noOfRows = Integer.parseInt(request.getParameter("noOfRows"));
            String chkboxVal="";
            for(i=j;i<=noOfRows;i++) {
                if((request.getParameter("chk_select"+i))==null) {
                    chkboxVal="notticked";
                }//if for checked closed
                else {
                    chkboxVal=request.getParameter("chk_select"+i);
                    if(chkboxVal.equals("ticked")) {
                        String LcId=request.getParameter("txtLcId"+i);
                        String LcDesc=request.getParameter("txtLcDesc"+i);                                  
                        LcDesc=LcDesc.trim();
                        String Rec_Status=request.getParameter("RecStatus"+i);
                        Statement st=reff.con.createStatement();
                        String qu="xxxxxxxxxxxxxxxxx";
                        st.executeUpdate(qu);
                    }//if chkbox closed
                }//else checked closed
                //j+=1;
            }//For Loop Closed
        } catch(SQLException sql) {
            request.setAttribute("error", ge+" General e Exception");
            reff.getServletConfig().getServletContext().getRequestDispatcher("/errjsp.jsp").forward(request,response);
        } catch(Exception ge) {
            request.setAttribute("error", ge+" General e Exception");
            reff.getServletConfig().getServletContext().getRequestDispatcher("/errjsp.jsp").forward(request,response);
        }           
        ResultSet rs1 = null;
        try {
            Statement st1 = reff.con.createStatement();
            rs1 = st1.executeQuery("Select * from xxxx");
            ResultSetMetaData rsm = rs1.getMetaData();
            int col_Count = rsm.getColumnCount();
            Vector vrow = new Vector();
            while(rs1.next()) {
                Vector vcol = new Vector();
                for(int i=1;i<=col_Count;i++) {
                    vcol.addElement(rs1.getObject(i));
                }
                vrow.addElement(vcol);
            } //while loop closed
            request.setAttribute("vrow",vrow);
            request.setAttribute("msg",msg);
            reff.getServletConfig().getServletContext().getRequestDispatcher("/xxx.jsp").forward(request,response);
        }catch(SQLException sqldel){}
        return ;
    }
}   

servlet 试图像这样调用这个类

mstLeastCountEdit ref1 = new mstLeastCountEdit();

它抛出空指针异常。我在课堂上仍然很草率,这是 10 年前开发的旧代码,有什么帮助吗??

4

3 回答 3

2

浏览代码...

HttpServletRequest request;

[...]

HttpSession session=request.getSession(true);
Integer ccode=(Integer) session.getAttribute("companycode");

此行应引发异常。request尚未分配,因此将被分配null,因此是 NPE。

servlet 通常会在请求和会话之间提供服务。甚至一次处理多个请求。因此,不要在 servlet 实例中存储请求、会话和相关数据。

(作为一种良好做法:制作字段private,并在可能的情况下final,以更常规的方式添加空格,对变量名使用驼峰大写(例如companyCode),并且不要缩写单词,特别是如果它们变得毫无意义。)

于 2010-01-12T02:53:12.163 回答
1

问题出在您的字段初始化中:

HttpServletRequest request; 
HttpSession session=request.getSession(true);
Integer ccode=(Integer) session.getAttribute("companycode");

第一行将“请求”初始化为空,第二行尝试使用它。

由于您将请求传递给方法,因此无需将其作为字段成员进行维护。将这两行移到方法的主体中。事实上,如果您将所有字段移动到方法的主体中(从而使它们成为局部变量),则无需在每个请求上创建新的“mstLeastCountEdit”;在 servlet 中,您可以维护对它的单个引用作为成员字段。

并且作为记录,Java 类名应该以大写字母开头。

于 2010-01-12T03:01:40.357 回答
0

线

HttpSession session=request.getSession(true);

在任何初始化请求之前调用,因此您试图尊重空指针。

于 2010-01-12T02:54:21.710 回答