无法克服exam.jsp中的空指针异常
我正在使用oracle数据库和html制作一个java在线考试系统。。。页面位于.jsp中。我正在eclipse中运行代码。除了空指针异常之外,其他一切似乎都正常工作。当我尝试运行无法克服exam.jsp中的空指针异常,jsp,Jsp,我正在使用oracle数据库和html制作一个java在线考试系统。。。页面位于.jsp中。我正在eclipse中运行代码。除了空指针异常之外,其他一切似乎都正常工作。当我尝试运行exam.jsp时,它是一个错误。我正在上传代码和错误截图: 当您尝试访问空对象时,会引发NullPointerException。在图片中的stacktrace中,您显然可以看到它将您指向第21行: int currentQuestion = ((Exam)request.getSession().getAttri
exam.jsp
时,它是一个错误。我正在上传代码和错误截图:
当您尝试访问空对象时,会引发
NullPointerException
。在图片中的stacktrace中,您显然可以看到它将您指向第21行:
int currentQuestion = ((Exam)request.getSession().getAttribute("currentExam")).getCurrentQuestion();
这意味着此行中的某些内容为空,而您仍在尝试访问它。您仅尝试访问request
、request.getSession()
和request.getSession().getAttribute(“currentExam”)
最有可能导致问题的原因是request.getSession().getAttribute(“currentExam”)
返回null。在运行代码之前,请尝试检查它是否不为null
你可能想看一看,然后
int currentQuestion = ((Exam)request.getSession().getAttribute("currentExam")).getCurrentQuestion();
can u please tell me about it in a bit of detail...
i am uploading another code ExamController.java which contains the
request.getSession().getAttribute("currentExam) method
package co.etest.quiz.controller;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 co.etest.quiz.Exam;
import co.etest.quiz.QuizQuestion;
/**
* Servlet implementation class ExamController
*/
@WebServlet("/exam")
public class ExamController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
boolean finish=false;
HttpSession session=request.getSession();
try
{
if(session.getAttribute("currentExam")==null)
{ session=request.getSession();
String selectedExam=
(String)request.getSession().getAttribute("exam");
System.out.println("Setting Exam "+selectedExam);
Exam newExam=new Exam(selectedExam);
session.setAttribute("currentExam",newExam);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd
HH:mm:ss a");
Date date = new Date();
String started=dateFormat.format(date);
session.setAttribute("started",started);
}
}catch(Exception e){e.printStackTrace();}
Exam exam=(Exam)request.getSession().getAttribute("currentExam");
if(exam.currentQuestion==0){
exam.setQuestion(exam.currentQuestion);
QuizQuestion q=exam.questionList.get(exam.currentQuestion);
session.setAttribute("quest",q);
}
String action=request.getParameter("action");
String radio=request.getParameter("answer");
int selectedRadio=-1;
exam.selections.put(exam.currentQuestion, selectedRadio);
if("1".equals(radio))
{
selectedRadio=1;
exam.selections.put(exam.currentQuestion, selectedRadio);
System.out.println("You selected "+selectedRadio);
}
else if("2".equals(radio))
{
selectedRadio=2;
exam.selections.put(exam.currentQuestion, selectedRadio);
System.out.println("You selected "+selectedRadio);
}
else if("3".equals(radio))
{
selectedRadio=3;
exam.selections.put(exam.currentQuestion, selectedRadio);
System.out.println("You selected "+selectedRadio);
}
else if("4".equals(radio))
{
selectedRadio=4;
exam.selections.put(exam.currentQuestion, selectedRadio);
System.out.println("You selected "+selectedRadio);
}
if("Next".equals(action)){
exam.currentQuestion++;
exam.setQuestion(exam.currentQuestion);
QuizQuestion q=exam.questionList.get(exam.currentQuestion);
session.setAttribute("quest",q);
}
else if("Previous".equals(action))
{ System.out.println("You clicked Previous Button");
exam.currentQuestion--;
exam.setQuestion(exam.currentQuestion);
QuizQuestion q=exam.questionList.get(exam.currentQuestion);
session.setAttribute("quest",q);
}
else if("Finish Exam".equals(action))
{ finish=true;
int result=exam.calculateResult(exam);
request.setAttribute("result",result);
request.getSession().setAttribute("currentExam",null);
request.getRequestDispatcher("result.jsp").forward(request,response);
}
if(finish!=true){
request.getRequestDispatcher("exam.jsp").forward(request,response);
}
}
}