0

I am trying to create an online quiz application, and have set out all of my java classes to make the quiz, and tested them within the console to ensure they are outputting what I require.

However when I visit my C1Test.jsp page, whereby a quiz should be generated, I am getting a NullPointerException thrown by glassfish, and after doing a bit of googling, I am still unsure why.

The stack trace can be found below, as well as my C1Test.jsp source code. Any help would be much appreciated, this is my final year project for my Computer Science degree, and I am unable to make any progress until this is fixed.

Thanks in advance.

    Info:   Closing WebApplicationContext for namespace 'dispatcher-servlet'startup date [Tue Nov 24 16:32:38 GMT 2015]; parent: Root WebApplicationContext
Info:   WebModule[null] ServletContext.log():Closing Spring root WebApplicationContext
Info:   Closing Root WebApplicationContext: startup date [Tue Nov 24 16:32:37 GMT 2015]; root of context hierarchy
Warning:   StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.C1Test_jsp._jspService(C1Test_jsp.java:101)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)

C1Test.jsp source code:

<%-- 
    Document   : C1 Test
    Created on : Nov 5, 2015, 6:47:35 PM
    Author     : Joe Perkins
--%>
<%@ page language="java" import="Dissertation.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>C1 Test</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    </head>

    <body>

        <nav class="navbar navbar-inverse">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand" href="HomePage.jsp">EduMath</a>
                </div>

                <div>
                    <ul class="nav navbar-nav">
                        <li><a href="Index.jsp">Home</a></li>
                        <li><a href="C1.jsp">C1 Revision</a></li>
                        <li><a href="C2.jsp">C2 Revision</a></li>
                        <li><a href="SkillsetDeveloper.jsp">Skillset Development</a></li>
                        <li><a href="About.jsp"> About</a></li>
                    </ul>
                </div>
            </div>
        </nav>



        <div class="container">
            <div class="jumbotron">
                <h1>C1 Revision Test</h1>    
            </div>
            <div class="container">
                <%
                    // Retrieve the current quiz question
                    int currQuestion = ((Exam)request.getSession().getAttribute("currentTest")).getCurrentQuestion();
                    System.out.println("Question No. " + currQuestion);
                %>

                Current Question ${sessionScope.quest.questionNo+1} / 10

                <span>${sessionScope.quest.question}</span><br/><br/>

                <form class ="form-horizontal" action="test" method="post">
                    <c:forEach var="choice" items="q${sessionScope.quest.questionOptions}" varStatus = "counter">
                        <input type="radio" name="answer" value="${counter.count}" >${choice}  <br/>
                    </c:forEach> <br />

                    <%
                        if(currQuestion > 0)
                        {
                    %>

                            <input type="submit" name="action" value="previous"/>
                    <%  } %>

                    <%
                        if(currQuestion < 19)
                        {
                            %>

                            <input type="submit" name="action" value ="next"/>
                            <% } %>

                            <input type="submit" name="action" value="Finish Exam"/>
                </form>




                </form>

            </div>
        </div>

    </body>
</html>

Edit: Different from question flagged as possible duplicate, I understand the basis of a NullPointerException, however was more looking for general advice on how to identify where the NullPointer is coming from based off the stack trace, as well as how best to set a session attribute.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
freshwaterjoe
  • 101
  • 3
  • 16
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Smittey Nov 24 '15 at 16:42
  • 1
    You should look into C1Test_jsp.java line 101. This generated source lies somewhere in the application directory within glassfish. `currentTest` could be null on a new session. You might prefer a coding style where one first comes into a servlet, and it forwards to a jsp, first putting all data in the request attributes. A bit of Model-View-Controller separation. – Joop Eggen Nov 24 '15 at 16:51
  • Hi @JoopEggen, thanks a lot for your advice, I have just checked that line: `int currQuestion = ((Exam)request.getSession().getAttribute("currentTest")).getCurrentQuestion();` However am still unsure what it is telling me is null, as I have set the session attribute currentTest to not null. – freshwaterjoe Nov 24 '15 at 17:00
  • 1
    So one needs to check the session gotten, then currentTest Exam. Check that there is no `<%@page session=false` on the other page. Dump the JSESSION_ID to see the real session ID. Currently you are relying on cookies in the browser maintaining the session. – Joop Eggen Nov 24 '15 at 17:15
  • @JoopEggen I really appreciate your help, but am still struggling to grasp what is going wrong. From what I understand, my NullPointerException is current coming from the `currentTest` session attribute being null, which means I need to declare it as something other than null (active?), however I am not sure where in my code I need to put this. Thanks for your help once again. – freshwaterjoe Nov 24 '15 at 17:42
  • It could also be the session being null. Simply first `Exam exam = ...; int curQuestion = exam.getCurrentQuestion();` would show far you come. Maybe suffices `int curQuestion = exam == null ? 0 : exam.getCurrentQuestion();` or such. – Joop Eggen Nov 24 '15 at 19:18

1 Answers1

0

Make sure that your session attribute currentTest is set before you do any operations on it's content.

YoYo
  • 9,157
  • 8
  • 57
  • 74
  • Hi JoD, Thanks for your help so far. Where is it best to set the session attribute `currentTest`? As well as my 4 java classes, I also have 4 controller java classes, including `examController` which looks after the exams. Would I need to set the session attribute `currentTest` within the `doGet` or `doPost` methods within that class? – freshwaterjoe Nov 24 '15 at 17:08