简体   繁体   中英

NullPointerException while retrieving data from the database

When I am trying to retrieve data from database it's showing NullPointerException.

Here is my servlet code:

public class displayData extends HttpServlet {
    String query;
    Connection conn;
    Statement st;
    ResultSet res;
    ConnectionManager dbconn;
    List lst= new ArrayList();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            dbconn= new ConnectionManager();
            conn=dbconn.getConnection();
            st=conn.createStatement();
            query="select * from reg";
            res=dbconn.getResultSet(query, conn);
            System.out.println(res);

           while(res.next())
                    {
                        lst.add(res.getString("uname"));
                        lst.add(res.getString("password"));    
                    }

           res.close();


        }catch(Exception e)
        {

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

        finally
        {
            request.setAttribute("EmpData", lst);
            response.sendRedirect("/success.jsp");
            RequestDispatcher rd= request.getRequestDispatcher("/success.jsp");
            rd.forward(request, response);
            lst.clear();
            out.close();
        }

    }

And Here is JSP Code for Retrieving Data from database using above servlet Code:

    <body>
        <h1>Employee List</h1>

            <% Iterator itr;%>
            <% List data = (List) request.getAttribute("EmpData");
            for(itr=data.iterator(); itr.hasNext();)  
            {              
            %>
    <tr>
            <% String s= (String) itr.next();%>
            <td><%=s%></td>
            <td><%=itr.next()%></td>
            <td><input type="submit" value="Edit" onclick="editRecord(<%=s%>;)"</td>
            <td><input type="submit" value="Delete" onclick="deleteRecord(<%=s%>;)"</td>
               <%}%>
    </tr>
    </body>

Please help me for solving this problem.

After seeing your Servlet code I found multiple issues,Lets go one by one

  1. I am not sure whether you defined your servlet as a servlet or not. Either do mapping in web.xml or add annotation like this
    @WebServlet("/displayData") public class displayData extends HttpServlet {
  2. In the servlet you don't have doGet and doPost method. So your method processRequest will not be invoked by the container. either put doGet and call your service method or rename your service method to doGet. Reference - Is it mandatory to have a doGet or doPost method?
  3. The disaster you done in try catch finally block. finally will be always called so there is no use writing the redirection code there as it will executed after catch also. In addition to that finally blocks first four lines are causing serious issues. You should not call both sendRedirect and forward one followed by another. Either do sendRedirect or do forward but not both at the same time. You will get this exception illegalstateexception-cannot-forward-after-response-has-been-committed
    Reference - java.lang.IllegalStateException: Cannot forward after response has been committed
  4. What to do forward or sendRedirect at last, In this case you have to use forward as its a server side action. Reference - Request Attributes not available in jsp page when using sendRedirect from a servlet
  5. Based on your path of jsp do forward. if your success and error jsp's are directly under Webcontent then do like this request.getRequestDispatcher("success.jsp");

Change these things and try again if not working let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM