Basically I have this function that allows people to search for other user based on their usernames. You type into the textbox id="search" and it will look for users with that username/similar username:
<form name="searchForm" action="searchUser" method="post">
<input type="text" id="search" name="search" placeholder="Search.." />
<input type="submit" id="searchButton" name="searchButton" value="Search!" onclick="searchUsers()"/>
</form>
This is the servlet SearchUserServlet that maps to the action searchUser.The static method searchUser(string) returns an arraylist of user objects. I am trying to pass this arraylist to QuickMatchResult.jsp:
public class SearchUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response){
try {
String name=request.getParameter("search");
ArrayList<User> userList= User.searchUser(name);
RequestDispatcher dispatcher=request.getRequestDispatcher("/QuickMatchResult.jsp");
request.getSession().setAttribute("userList",userList);
dispatcher.forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
This is the QuickMatchResult.jsp where it is forwarded. I'm trying to show all the usernames of the user objects in the arraylist:
<table id="searchResults">
<% ArrayList<User> userList= new ArrayList<User>();
userList=(ArrayList<User>)request.getAttribute("userList");
for (int i=0; i<userList.size();i++){
User user=userList.get(i);
%>
<tr>
<td><%=user.getUserName()%></td>
</tr>
<%
}
%>
</table>
However when I try to run this, I get a null pointer exception. Any idea why?