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

サーブレットはこのクラスを次のように呼び出そうとしています

mstLeastCountEdit ref1 = new mstLeastCountEdit();

nullpointer 例外をスローします。私はまだクラスでずさんです。これは 10 年前に開発された古いコードです。

4

3 に答える 3

2

コードをざっと見てみると…

HttpServletRequest request;

[...]

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

この行は例外をスローする必要があります。request割り当てられていないため、割り当てられるnullため、NPE.

サーブレットは通常、リクエストとセッションの間で提供されます。一度に複数のリクエストを処理することもできます。したがって、リクエスト、セッション、および関連データをサーブレット インスタンス内に格納しないでください。

(適切な方法として、フィールドprivateを作成し、可能なfinal場合はより慣習的にスペースを追加し、変数名にキャメル キャップを使用し (たとえば、companyCode)、単語が無意味になる場合は特に単語を省略しないでください。)

于 2010-01-12T02:53:12.163 に答える
1

問題はフィールドの初期化にあります。

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

最初の行は「request」をnullに初期化し、2番目の行はそれを使用しようとします。

リクエストをメソッドに渡すため、フィールドメンバーとして保持する必要はありません。これらの2行をメソッドの本体に移動します。実際、すべてのフィールドをメソッドの本体に移動し、ローカル変数にする場合は、リクエストごとに新しい「mstLeastCountEdit」を作成する必要はありません。サーブレットでは、メンバーフィールドとしてサーブレットへの単一の参照を維持できます。

また、記録として、Javaクラス名は大文字で始める必要があります。

于 2010-01-12T03:01:40.357 に答える
0

この線

HttpSession session=request.getSession(true);

何かがリクエストを初期化する前に呼び出されるため、ヌルポインターを参照しようとしています。

于 2010-01-12T02:54:21.710 に答える