2016-04-14 12 views
0

私はoracleデータベースとhtmlを使ってJavaでオンライン検査システムを作っています...ページは.jspにあります。私はeclipse..everythingでコードを実行しています.を実行しようとするとエラーが発生するnullポインタの例外を除いて正常に動作するようです。あなたがnullのオブジェクトにアクセスしようとするとNullPointerExceptionがスローされ enter image description hereexam.jspのnullポインタ例外を克服できません

<%@ page language="java" import="co.etest.quiz.Exam" 
contentType="text/html;  charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Quiz</title> 
<style type="text/css"> 
body { 
background:  
url("${pageContext.request.contextPath}/images/background.jpg"); 
} 
</style> 

</head><br/> 
<body> 
<div style="position:absolute;left:50px;top:20px"> 

<% 
int currentQuestion=  
(`enter code here` (Exam)request.getSession().getAttribute("currentExam")).getCurrentQuestion(); 
// System.out.println("Question Number "+currentQuestion+ " retrieved "); 
%> 
Current Question ${sessionScope.quest.questionNumber+1}/10 
</div> 


<div style="position:absolute;width:1000px;padding:25px; 
height: 200px;border: 1px solid green ;left:100px;top:60px"> 
<span>${sessionScope.quest.question}</span><br/><br/> 
<form action="exam" method="post" > 
<c:forEach var="choice" items="${sessionScope.quest.questionOptions}" 
varStatus="counter"> 
<input type="radio" name="answer" value="${counter.count}" >${choice} <br/> 
</c:forEach> <br/> 

<% 
if(currentQuestion > 0) 
{ 
%> 
<input type="submit" name="action" value="Previous" /> 
<%} %> 

<% 
if(currentQuestion < 9) 
{ 
%> 
<input type="submit" name="action" value="Next" /> 
<%} %> 
<input type="submit" name="action" value="Finish Exam" /> 

</form> 
</div> 


</body> 
</html> 

答えて

0

:私はコードとエラーのスクリーンショットをアップロードしています。

int currentQuestion = ((Exam)request.getSession().getAttribute("currentExam")).getCurrentQuestion(); 

この行の何かがnullであることを意味し、あなたはまだそれにアクセスしようとしている:写真のスタックトレースでは、明らかに、あなたが21行目に指しています見ることができます。 requestrequest.getSession()、およびrequest.getSession().getAttribute("currentExam"))にアクセスしようとしています。

ほとんどの場合、問題の原因はrequest.getSession().getAttribute("currentExam"))がnullを返すことです。コードを実行する前にnullでないかどうかを調べてみてください。

herehereをご覧ください。

0
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); 
    } 

    } 

    } 
関連する問題