java - nullpointer exception when initializing a class in servlet -
class mstleastcountedit { mstleastcount reff; httpservletrequest request; httpservletresponse response; string msg = ""; string lcdesc=""; int = 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 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 * 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 trying call class this
mstleastcountedit ref1 = new mstleastcountedit();
and throws nullpointer exception. still sloppy on class , , old code developed 10 years back, ??
glancing through code...
httpservletrequest request; [...] httpsession session=request.getsession(true); integer ccode=(integer) session.getattribute("companycode");
this line should throw exception. request
has not been assign , null
, hence npe.
a servlet typically served between request , sessions. handling multiple requests @ once. therefore, not store requests, sessions , related data within servlet instance.
(as matter of practice: make fields private
, possible final
, add spaces more conventionally, use camel caps variable names (for instance companycode
) , don't abbreviate words particularly if become meaningless.)
Comments
Post a Comment