2

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.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79

1 Answers1

0

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

Narendra Jaggi
  • 1,297
  • 11
  • 33
  • I have solved above problems in my code but i still getting error.....Warning: 'StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException at org.apache.jsp.success_jsp._jspService(success_jsp.java:66)' – Nitin Thakor Jan 13 '16 at 10:41
  • Just do a null check in jsp if you get list as null – Narendra Jaggi Jan 13 '16 at 19:39
  • Thanks For Response .......its Solved ...... M trying to call JSP file directly without setting attribute.. i have to call servlet first.......... thanks............ – Nitin Thakor Jan 16 '16 at 05:00
  • so all time you was hitting jsp directly?? – Narendra Jaggi Jan 16 '16 at 05:04
  • yah.....its working during inserting Data .......but during during displaying data its not working.... – Nitin Thakor Jan 16 '16 at 05:13