Reputation: 1
Ive tried everything and i cant seem to figure why i keep getting "list is null" when i run my jsp file. Here is my servlet file
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Question;
@WebServlet("/DrivingTestBrowser")
public class DrivingTestBrowser extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
List<Question> entry = new ArrayList<>();
ServletContext context = getServletContext();
String file = getServletContext().getRealPath("/WEB-INF/Drivingtest.txt");
Scanner in = new Scanner(file);
while (in.hasNextLine()){
Question question = new Question();
question.setDescription(in.nextLine());
question.setAnswerA(in.nextLine());
question.setAnswerB(in.nextLine());
question.setAnswerC(in.nextLine());
question.setCorrectAnswer(Integer.parseInt(in.nextLine()));
in.nextLine();
entry.add(question);
}
in.close();
context.setAttribute("entries", entry);
}
public DrivingTestBrowser() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id;
HttpSession session = request.getSession();
if (session.getAttribute("id") == null) {
id = 0;
} else {
id = (int) session.getAttribute("id");
id++;
}
session.setAttribute("id", id);
request.setAttribute("SessionID", id);
request.getRequestDispatcher("DrivingTestBrowser.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
and this is my jsp file
<%@page import="java.util.List"%>
<%@page import="model.Question"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>DrivingTestBrowser</title>
</head>
<body>
<%
ServletContext context = request.getServletContext();
List<Question> list = (List<Question>)context.getAttribute("entries");
int Sessionid = 0;
if (request.getAttribute("Sessionid")!= null) {
Sessionid = Integer.parseInt(request.getAttribute("Sessionid").toString())% list.size();
}
%>
<p><%=list.get(Sessionid).getDescription()%></p>
<p>1:<%=list.get(Sessionid).getAnswerA()%></p>
<p>2:<%=list.get(Sessionid).getAnswerB()%></p>
<p>3:<%=list.get(Sessionid).getAnswerC()%></p>
<p>Correct Answer:<%=list.get(Sessionid).getCorrectAnswer()%></p>
<p><a href="DrivingTestBrowser?Sessionid=<%=Sessionid++%>">Next</a></p>
</body>
</html>
This is the error im getting "java.lang.NullPointerException: Cannot invoke "java.util.List.get(int)" because "list" is null"
My list of questions is null
Upvotes: 0
Views: 481
Reputation: 9463
There is a chance that by the time the application has deployed and you run the request for the JSP page the servlet has not been loaded at all (and with that, also not initialized). This is related to lazy loading which is configured per default.
You could verify by first invoking your servlet's URL, then going to the JSP.
To make your servlet load without having received any request, you need to configure the loadOnStartup by giving it a sequence number:
@WebServlet(
urlPatterns = "/DrivingTestBrowser",
loadOnStartup = 1
)
Since you populate the list of questions only once at startup time, it may be even better to not use a servlet at all but a ServletContextListener's contextInitialized() method.
Upvotes: 1
Reputation: 330
Minor error in "entry" in the Servlet should be...
List<Question> entry = new ArrayList<Question>();
Note: An attribute stored by the server Servlet or jsp application scope must be a class as Object not an interface.
....And is that a seriously mad way of using a local host to test input data that should be acquired by a ServletInputStream?
If you want to load test data at startup of the application, Make a ContextInit class similar to a Servlet and setAttribute in that, no need for the scanner.
Upvotes: -1