views:

75

answers:

3
 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 ;
    }
}   

the servlet is trying to call this class like this

mstLeastCountEdit ref1 = new mstLeastCountEdit();

and it throws nullpointer exception. i am still sloppy on the class , and this is a old code developed 10 years back, any will help ??

+2  A: 

Glancing through the code...

HttpServletRequest request;

[...]

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

This line should throw an exception. request has not been assign and so will be null, hence the NPE.

A servlet will typically be served between request and sessions. Even handling multiple requests at once. Therefore, do not store requests, sessions and related data within a servlet instance.

(As a matter of good practice: make fields private and where possible final, add spaces more conventionally, use camel caps for variable names (for instance companyCode) and don't abbreviate words particularly if they become meaningless.)

Tom Hawtin - tackline
Spot on. I would however be *really* surprised if this "old code" worked that 10 years long.
BalusC
:-) me also , my overall programming experience is very much less than this code. any way i will try and get back with result.
sansknwoledge
A: 

The line

HttpSession session=request.getSession(true);

is called before anything initializes request, so therefore you're trying to deference a null pointer.

Paul Tomblin
+1  A: 

The problem is in your field initializations:

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

The first line initializes "request" to null, the second tries to use it.

Since you are passing the request into the method, there is no need to maintain it as a field members. Move those two lines into the body of the method. In fact, if you move all the fields into the body of the method - therby making them local variables - then there is no need to create a new "mstLeastCountEdit" on each request; in the servlet you can maintain a single reference to it as a member field.

And for the record, Java class names should start with a Capital letter.

beltorak