更新和删除记录时java servlet中的NullPointerException

时间:2014-05-07 10:07:07

标签: java jsp servlets

我开发了浏览器前端,用于显示,编辑和更新数据库中的记录。以前我有不同的servlet用于执行插入,编辑和更新记录。现在我希望将所有servlet放入一个名为“DisplayData”的servlet中,并在login.JSP表单和“edit.jsp”中分别使用隐藏字段,以便servlet可以根据隐藏参数执行登录操作或更新操作。 / p>

该代码适用于只允许注册用户的登录操作,但是当我尝试编辑记录并更新它时,它显示了java.lang.Nullpointer异常。当我尝试删除记录时,它显示相同的错误。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{

HttpSession session = request.getSession(true);
HttpSession usersession = request.getSession(true);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String query;
Connection conn;
Statement stmt;
ResultSet res;
DatabaseConnection dbconn;

String username="";
String hiddenname = request.getParameter("hiddenname"); 
System.out.println(hiddenname);



    username = request.getParameter("username");
    System.out.println(username);
    String password = request.getParameter("password");
    System.out.println(password);
    usersession.setAttribute("uname", username);
    usersession.setAttribute("upass", password);
    Connection con = dbconnection.getCon();
    System.out.println(con);
    PreparedStatement statemt = null;

    if (hiddenname.equals("login"))
            {

        try {
    statemt = con.prepareStatement("select User_name,Password from login_details where User_name = ? and Password = ?");
    System.out.println(statemt);
    statemt.setString(1, username);
    statemt.setString(2, password); 
    ResultSet rs = statemt.executeQuery();
        if (rs.next() == false) 
            {
                out.write("Invalid user name or password. Please press back button to login again");

            }
        else
        {
            List lst=new ArrayList();       
            String login_name,login_password;
            try
            {

                    login_name=(String) session.getAttribute("uname");
                    login_password=(String) session.getAttribute("upass");
                    request.setAttribute("UserName", login_name);
                    request.setAttribute("UserPassword", login_password);
                    dbconn=new DatabaseConnection();        
                    conn=dbconn.setConnection();
                    stmt=conn.createStatement();
                    query="select * from mpi";
                    res=dbconn.getResultSet(query, conn);
                    while(res.next())
                    {

                        lst.add(res.getString("UniqueID"));
                        lst.add(res.getString("Phylum"));
                        lst.add(res.getString("Family"));
                        lst.add(res.getString("Genus"));
                        lst.add(res.getString("NCBI_Taxnomy_ID"));
                        lst.add(res.getString("16s_Sanger_seq"));
                        lst.add(res.getString("Genome_Sequencing_Batch"));
                        lst.add(res.getString("Stock_number"));
                        lst.add(res.getString("Stock_Location"));
                        lst.add(res.getString("Soil_batch"));
                        lst.add(res.getString("Host"));
                        lst.add(res.getString("Operator"));
                        lst.add(res.getString("GPS_coordinates"));
                        lst.add(res.getString("Greenhouse_or_Natural_sites"));
                        lst.add(res.getString("Isolation_procedure"));
                        lst.add(res.getString("Date_of_isolation"));
                        lst.add(res.getString("Previous_Ids"));
                        lst.add(res.getString("Compartment"));
                        lst.add(res.getString("Publication"));
                        lst.add(res.getString("Strain_Derivatives"));
                        lst.add(res.getString("Growth_conditions"));
                        lst.add(res.getString("Natural_antibiotic_resistance"));
                        lst.add(res.getString("Colony_morphology"));
                    }
                                res.close(); 
                }
                catch(Exception e)
                {
                    e.printStackTrace();        
                }
                finally
                {
                    request.setAttribute("UserData", lst);
                    RequestDispatcher rd=request.getRequestDispatcher("/displayrecord.jsp");
                    rd.forward(request, response);
                    lst.clear();
                    out.close();
                }
            }           
                    con.close();
    } 


            catch (SQLException e) 
                {
                    e.printStackTrace();
                }

    }

    else if(hiddenname.equals("update"))
    {
        String UniqueID,Phylum,Family,Genus,NCBI_Taxnomy_ID,s16_Sanger_seq,Genome_Sequencing_Batch,Stock_number,
        Stock_Location,Soil_batch,Host,Operator,GPS_coordinates,Greenhouse_or_Natural_sites,
        Isolation_procedure,Date_of_isolation,Previous_Ids,Compartment,Publication,Strain_Derivatives,Growth_conditions,
        Natural_antibiotic_resistance,Colony_morphology,Comments;   
        response.setContentType("text/html;charset=UTF-8");
        try{
            dbconn=new DatabaseConnection();        
            conn=dbconn.setConnection();
            System.out.println("conn");
            UniqueID=request.getParameter("UniqueID");
            Phylum=request.getParameter("Phylum");          
            Family=request.getParameter("Family");          
            Genus=request.getParameter("Genus");        
            NCBI_Taxnomy_ID=request.getParameter("NCBI_Taxnomy_ID");        
            s16_Sanger_seq=request.getParameter("s16_Sanger_seq");
            System.out.println(s16_Sanger_seq);
            Genome_Sequencing_Batch=request.getParameter("Genome_Sequencing_Batch");
            Stock_number=request.getParameter("Stock_number");
            Stock_Location=request.getParameter("Stock_Location");
            Soil_batch=request.getParameter("Soil_batch");
            Host=request.getParameter("Host");
            Operator=request.getParameter("Operator");
            GPS_coordinates=request.getParameter("GPS_coordinates");
            Greenhouse_or_Natural_sites=request.getParameter("Greenhouse_or_Natural_sites");
            Isolation_procedure=request.getParameter("Isolation_procedure");
            Date_of_isolation=request.getParameter("Date_of_isolation");
            Previous_Ids=request.getParameter("Previous_Ids");
            Compartment=request.getParameter("Compartment");
            Publication=request.getParameter("Publication");
            Strain_Derivatives=request.getParameter("Strain_Derivatives");
            Growth_conditions=request.getParameter("Growth_conditions");
            Natural_antibiotic_resistance=request.getParameter("Natural_antibiotic_resistance");
            Colony_morphology=request.getParameter("Colony_morphology");

            stmt=conn.createStatement();
            query="update mpi set Phylum='"+Phylum+"',Family='"+Family+"',Genus='"+Genus+"',NCBI_Taxnomy_ID='"+NCBI_Taxnomy_ID+"',16s_Sanger_seq='"+s16_Sanger_seq+"',Genome_Sequencing_Batch='"+Genome_Sequencing_Batch+"',Stock_number='"+Stock_number+"',Stock_Location='"+Stock_Location+"',Soil_batch='"+Soil_batch+"',Host='"+Host+"',Operator='"+Operator+"',GPS_coordinates='"+GPS_coordinates+"',Greenhouse_or_Natural_sites='"+Greenhouse_or_Natural_sites+"',Date_of_isolation='"+Date_of_isolation+"',Isolation_procedure='"+Isolation_procedure+"',Previous_Ids='"+Previous_Ids+"',Compartment='"+Compartment+"',Publication='"+Publication+"',Strain_Derivatives='"+Strain_Derivatives+"',Growth_conditions='"+Growth_conditions+"',Natural_antibiotic_resistance='"+Natural_antibiotic_resistance+"',Colony_morphology='"+Colony_morphology+"' where UniqueID='"+UniqueID+"'";
            System.out.println(query);
            int i=stmt.executeUpdate(query);
        }
        catch(Exception e)
        {

        }
        finally{

            RequestDispatcher rd=request.getRequestDispatcher("/displayrecord.jsp");
            rd.forward(request, response);
            out.close();
        }


    }else if(hiddenname.equals("delete"))
    {
        try{
                                   id=request.getParameter("hidden_delete");
            System.out.println(id);
            dbconn=new DatabaseConnection();
            conn=dbconn.setConnection();
            System.out.println(conn);
            stmt=conn.createStatement();
            System.out.println(stmt);
            query="delete from mpi where UniqueID="+id;
            int i=stmt.executeUpdate(query);

        }catch (Exception e)
        {

        }finally{
            RequestDispatcher rd=request.getRequestDispatcher("DisplayData");
            rd.forward(request, response);
            out.close();
        }
    }
}}

有人能告诉我,我的错误在于谎言。 这是我的displayrecord.jsp的JSP代码

`<body><input type="button" onclick="tableToExcel('table_id', 'W3C Example Table')" value="Export to Excel">
<TABLE  id="table_id" align="Center" border="1px" width="80%">
    <thead>
    <tr>
    <th><b>UniqueID</b>
    <form action="SearchId" method="post">
    <input type="hidden" name="hiddenname" value="hidden_uid" >
    <input class="ignore" type="text" name="uid" id="uid">
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Phylum</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="phylum" id="phylum">
    <input type="hidden" name="hiddenname" value="hidden_phylum" >
    <input class="ignore" type="submit"  value="Search">
    </form>
    </th>
    <th><b>Family</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="family" id="family">
    <input type="hidden" name="hiddenname" value="hidden_family" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Genus</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="genus" id="genus">
    <input type="hidden" name="hiddenname" value="hidden_genus" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>NCBI Taxonomy ID</b>
    <form action="SearchId" method="post">
    <input type="hidden" name="hiddenname" value="hidden_taxid" >
    <input class="ignore" type="text" name="taxid" id="taxid">
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>16s Sanger Seq</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="16ssangerseq" id="16ssangerseq">
    <input type="hidden" name="hiddenname" value="hidden_16ssangerseq" >
    <input class="ignore" type="submit"  value="Search">
    </form>
    </th>
    <th><b>Genome Sequencing Batch</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="genomeseqbatch" id="genomeseqbatch">
    <input type="hidden" name="hiddenname" value="hidden_genomeseqbatch" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Stock number</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Stock_number" id="Stock number">
    <input type="hidden" name="hiddenname" value="hidden_Stocknumber" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Stock Location</b>
    <form action="SearchId" method="post">
    <input  type="hidden" name="hiddenname" value="hidden_StockLocation" >
    <input class="ignore" type="text" name="Stock_Location" id="Stock Location">
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Soil batch</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Soil_batch" id="Soil batch">
    <input type="hidden" name="hiddenname" value="hidden_Soilbatch" >
    <input class="ignore" type="submit"  value="Search">
    </form>
    </th>
    <th><b>Host</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="host" id="host">
    <input type="hidden" name="hiddenname" value="hidden_host" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Operator</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="operator" id="operator">
    <input type="hidden" name="hiddenname" value="hidden_operator" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>GPS co-ordinates</b>
    <form action="SearchId" method="post">
    <input  type="hidden" name="hiddenname" value="hidden_GPS_coordinates" >
    <input class="ignore" type="text" name="GPS_coordinates" id="GPS co-ordinates">
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Green House Sites</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Green_House_Sites" id="Green House Sites">
    <input type="hidden" name="hiddenname" value="hidden_Green_House_Sites" >
    <input class="ignore" type="submit"  value="Search">
    </form>
    </th>
    <th><b>Isolation procedure</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Isolation_procedure" id="Isolation procedure">
    <input type="hidden" name="hiddenname" value="hidden_Isolation_procedure" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Date of isolation</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Date_of_isolation" id="Date of isolation">
    <input type="hidden" name="hiddenname" value="hidden_Date_of_isolation" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Previous Ids</b>
    <form action="SearchId" method="post">
    <input type="hidden" name="hiddenname" value="hidden_Previous_Ids" >
    <input class="ignore" type="text" name="Previous_Ids" id="Previous_Ids">
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Compartment</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Compartment" id="Compartment">
    <input type="hidden" name="hiddenname" value="hidden_Compartment" >
    <input class="ignore" type="submit"  value="Search">
    </form>
    </th>
    <th><b>Publication</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Publication" id="Publication">
    <input type="hidden" name="hiddenname" value="hidden_Publication" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Strain Derivatives</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Strain_Derivatives" id="Strain Derivatives">
    <input type="hidden" name="hiddenname" value="hidden_Strain_Derivatives" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Growth conditions</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Growth_conditions" id="Growth conditions">
    <input type="hidden" name="hiddenname" value="hidden_Growth_conditions" >
    <input class="ignore" type="submit"  value="Search">
    </form>
    </th>
    <th><b>Natural antibiotic resistance</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Natural_antibiotic_resistance" id="Natural antibiotic resistance">
    <input type="hidden" name="hiddenname" value="hidden_Natural_antibiotic_resistance" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th>
    <th><b>Colony morphology</b>
    <form action="SearchId" method="post">
    <input class="ignore" type="text" name="Colony_morphology" id="Colony morphology">
    <input type="hidden" name="hiddenname" value="hidden_Colony_morphology" >
    <input class="ignore" type="submit" value="Search">
    </form>
    </th></tr>
    </thead>
    <tbody id="fbody">
    <%Iterator itr;%>
    <%List data=(List) request.getAttribute("UserData");
    for(itr=data.iterator();itr.hasNext();)
    {%>
        <tr>
            <% String s= (String) itr.next(); %> 
            <td><%=s %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>
            <td><%=itr.next() %></td>

     <c:choose>
             <c:when test="${UserName!= 'admin'&& UserPassword!='admin'}">
        <form id="edit" action="EditRecord" method="post" >
            <td><input type="hidden" name="hidden_edit" id="edit_id" value="<%=s %>"/> 
             <input class="ignore" type="submit" disabled="disabled" value="Edit" name="edit"> </td>
        </form>
        <form id="delete" action="DeleteRecord" method="post" >
            <td><input type="hidden" name="hidden_delete" id="delete_id" value="<%=s %>"/>
            <input class="ignore" type="submit" disabled="disabled" value="delete" name="delete"> </td>
        </form>
    </c:when>
    <c:otherwise>
        <form id="edit" action="EditRecord" method="post" >
            <td><input type="hidden" name="hidden_edit" id="edit_id" value="<%=s %>"/> 
            <input class="ignore" type="submit"  value="Edit" name="edit"> </td>
        </form>
        <form id="delete" action="DeleteRecord" method="post" >
            <td><input type="hidden" name="hidden_delete" id="delete_id" value="<%=s %>"/>
            <input class="ignore" type="submit"  value="delete" name="delete"> </td>
        </form>
    </c:otherwise>
</c:choose>     
        </tr>                   
    <%}%>
    </tbody>    
</TABLE>

我尝试删除后获得的例外情况如下:

    java.lang.NullPointerException
DisplayData.DisplayData.doPost(DisplayData.java:85)
javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
DeleteRecord.DeleteRecord.doPost(DeleteRecord.java:61)
javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

1 个答案:

答案 0 :(得分:0)

这一行

String hiddenname = request.getParameter("hiddenname"); 

可能导致hiddenname beeing null(请参阅ServletRequest#getParameter

所以当你这样做时

if (hiddenname.equals("login"))

你得到一个NullPointerException

将其更改为yoda notation(无论如何这是最好的协议)

if ("login".equals(hiddenname))

...

else if("update".equals(hiddenname))

"login"(一般来说是一个字符串文字)永远不能为空,你不会得到一个NPE。或者确保请求参数始终存在。