0

I am trying to get a table in my JSP page to show data from my get method in servlet, but I am getting a NullPointerException. I don't know why it is not connecting to my servlet method. I also linked my controller in the web.xml.

Here is my code in my jsp page:

<form action="/Controller" method="get"> 
<% 
            
 List<Animal> animal = (List) request.getAttribute("animal");

 if(animal != null) {
 for (Animal a : animal)  {
  %>
    
<tr>
            
<td><%=a.getAnimalId()%></td> 
<td><%=a.getAnimalName() %></td>    
</tr>
<%  } 

And here is my code in my servlet

private void lists(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String data = request.getParameter("dataType");
    String url ="";
    
    if(data.equals("animal")) {
        List<Animal> theList = facade.findAllAnimal();
        request.setAttribute("list", theList);
        
        url = "/Animal.jsp";
    
    }else if (data.equals("")) {
        
    }else if (data.equals("")) {
        
    }
    
     dispatcher = getServletContext().getRequestDispatcher(url); 
     dispatcher.forward(request, response); 
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");
    
    if(action == null) {
        action = "LIST";
    }
    
    switch(action) {
        
        case "LIST":
            lists(request, response);
            break;
            
        case "DELETE":
            delete(request, response);
            break;
            
        default:
            lists(request, response);
            break;
            
    }
}

0 Answers0