0

I'm working on a project using Spring MVC. I create many CRUDs which were working fine.But for that one the save does't work.I have two classes Exam and Question. I choose the exam then in have a form where to add questions for this exam. the error is:

exception
     org.springframework.web.util.NestedServletException: Request processing  failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  cause mère

   java.lang.NullPointerException
org.inpt.elearning.controllers.ExpertController.saveQuestionExam(ExpertController.java:175)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

the method in my controller:

       @RequestMapping(value="/saveQuestionExam")
public String saveQuestionExam(Question q,Model model){
    if(q.getId_question()!=null){
        metier.modifierQuestion(q);
    }
    else
    metier.ajouterQuestion(q,q.getExamQuestion().getIdExam());
    model.addAttribute("question",new Question());
    model.addAttribute("questions",metier.listQuestionParExam(q.getExamQuestion().getIdExam()));
    model.addAttribute("exams",metier.listExam());
    return "ajouterQuestions";
}

My ajouterQuestions.jsp file:

             <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
   <%@taglib uri="http://www.springframework.org/tags/form" prefix="f" %>
     <head>
     <link rel="stylesheet" type="text/css" href=" <%=request.getContextPath()%>/resources/css/style1.css" >
 </head>
   <div id="formQuestionExam" class="cadre">
   <f:form  modelAttribute="question" action="saveQuestionExam"
 method="post" enctype="multipart/form-data">
    <table>
        <tr>
             <td>ID question</td>
             <td><f:input path="id_question"/></td>
             <td><f:errors path="id_question" cssClass="errors"></f:errors>       </td>
        </tr>

        <tr>
             <td>Formulation</td>
             <td><f:textarea path="formulation"/></td>               
             <td><f:errors path="formulation" cssClass="errors"></f:errors></td>
        </tr>
        <tr>
             <td>Choix 1</td>
             <td><f:input path="choix1_question"    /></td>                 
             <td><f:errors path="choix1_question" cssClass="errors">   </f:errors></td>
        </tr>
         <tr>
             <td>Choix 2</td>
             <td><f:input path="choix2_question"    /></td>                 
             <td><f:errors path="choix2_question" cssClass="errors"></f:errors></td>
        </tr>
      <tr>
             <td>Choix 3</td>
             <td><f:input path="choix3_question"    /></td>                 
             <td><f:errors path="choix3_question" cssClass="errors"></f:errors></td>
        </tr>
         <tr>
             <td>La réponse correcte</td>
             <td><f:input path="rep_correcte" /></td>
             <td><f:errors path="rep_correcte" cssClass="errors"></f:errors></td>
        </tr>              
         <tr>   
        <td><input type="submit" value="Save"/></td>
        </tr>
  </table>

   </f:form>
        </div>

     <div id="tabCours" class="cadre">
<table class="tab1">
    <tr>
        <th>ID</th><th>Formulation</th><th>choix1</th><th>Choix2</th>< th>choix3</th><th>Réponse correcte</th>          
    </tr>
    <c:forEach items="${questions}" var="q">
         <tr>
          <td>${q.id_question}</td>             
          <td>${q.formulation}</td>
          <td>${q.choix1_question}</td>
          <td>${q.choix2_question}</td>
          <td>${q.choix3_question}</td>
          <td>${q.rep_correcte}</td>                       
          <td><a href="suppQuest?id_question=${q.id_question}">Supprimer</a></td>   
          <td><a href="editQuest?id_question=${q.id_question}">Editer</a>  </td>  
         </tr>         
        </c:forEach>
</table>

Kaoutar Laz
  • 61
  • 1
  • 2
  • 9
  • Check ExpertController.java line 175, something is null on this line and you are calling a method on the null object, what is the code on this line?? – Petter Friberg Jan 01 '16 at 22:12
  • model.addAttribute("questions",metier.listQuestionParExam(q.getExamQuestion().getIdExam())); – Kaoutar Laz Jan 01 '16 at 22:17
  • I undrestand that i point on somthing null, but i've check everything i reknow why. – Kaoutar Laz Jan 01 '16 at 22:18
  • listQuestionParExam(q.getExamQuestion().ge‌​tIdExam() should give the questions of the exam where the id is the same in the question added@PetterFriberg – Kaoutar Laz Jan 01 '16 at 22:19
  • metier.ajouterQuestion(q,q.getExamQuestion().getIdExam()); the problem is here i reknow how to add the id of this exam when i point to it @PetterFriberg – Kaoutar Laz Jan 01 '16 at 22:35
  • The linked question provides *comprehensive* instructions on how to diagnose and fix NPEs. In your case, you need to figure out which of about 4 *possible* causes of the NPE (on that line) is the actual cause. There is insufficient information / context for us to do that for you. – Stephen C Jan 01 '16 at 23:17
  • Now i know the problem but i dont know how to fix it. when i choose the exam in which id like to add question my url is like this :http://localhost:7070/elearning/expert/ajouterQuestionExam?idExam=1 now i don't know how to get this id from url to use it in: metier.ajouterQuestion(q,IdExam); @Stephen – Kaoutar Laz Jan 01 '16 at 23:23
  • I don't understand what you are saying. Your form can't possibly generate a request with an `idExam` parameter, and the `RequestMapping` certainly doesn't extract an `idExam` parameter from the request. – Stephen C Jan 02 '16 at 00:19
  • No it does but i dont knom how to extract this idExam from the url to use it the method ajouterQuetion(q,IdExam) @StephenC – Kaoutar Laz Jan 02 '16 at 00:31
  • 1) This is a different question to your original one. Ask a new question providing the relevant information; e.g. the real URL, the real code and the real JSP ... that generates the URL. 2) Before you ask a new Question, search for and read the Spring documentation on how `@RequestMapping` works ... 'cos >>that<< should answer your question. – Stephen C Jan 02 '16 at 00:40
  • Yes that what i did I post another question. thanrs @StephenC – Kaoutar Laz Jan 02 '16 at 00:42

0 Answers0