Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am getting error in my controller Saying Null Pointer Exception while When I don't perform the testing. Everything works fine.

Controller :
@RequestMapping(value = "/studentinsection/{sectionId}", method = RequestMethod.GET)
    public ModelAndView studentInSectionForm(@ModelAttribute("studentInSectionFormData") StudentInSectionForm studentInSectionFormData,
                                             @PathVariable Integer sectionId,
                                             ModelMap model) {
        ArrayList<StudentInSections> studentInSectionList = (ArrayList<StudentInSections>)
                studentInSectionsService.retrieveAllStudentInSections(sectionId, 1);

        StudentSection studentSection = studentSectionService.retrieveStudentSection(sectionId);

        logger.info("section Name is:" + studentSection.getSectionName());

        ArrayList<User> userList = new ArrayList<User>();
        for (StudentInSections studentInSections : studentInSectionList) {
            String studentName =
                    (userService.retrieveUserName(studentInSections.getStudentId(), 1));
            User users = userService.retrieveUser(studentName);
            userList.add(users);
        }


        logger.info("sectionId is " + sectionId);

        ArrayList<User> allStudents = (ArrayList<User>)
                userService.retrieveAllStudents();

        studentInSectionFormData.setStudentInSectionList(studentInSectionList);
        model.addAttribute("studentList", allStudents);
        model.addAttribute("userList", userList);
        model.addAttribute("studentSectionName", studentSection.getSectionName());
        model.addAttribute("studentSectionId", studentSection.getSectionId());
        return new ModelAndView("studentinsection", "studentInSectionFormData", studentInSectionFormData);
    }


Testing is as follow:

    @Test
    public void testStudentInSectionForm() throws Exception {
        mockMvc.perform(get("/studentinsection/1"))
                .andExpect(status().isFound())
                .andExpect(redirectedUrl("studentinsection"));
    }

this is passing everything into the controller fine even sectionId is getting printed 1 in logger than also studentin sectionList returns nullMointerException. help me to resolve my problem.. Thanx

share|improve this question
    
I have set everything fine. All entity works. A simple testing having no values in controller is working fine. –  kushal jain Aug 7 '13 at 6:14

1 Answer 1

It slooks like the context is not being loaded correctly. What is the exception stacktrace.

You can also view the request if you do :

  mockMvc.perform(get("/studentinsection/1"))
   .andExpect(status().isFound())
   .andDo(print())
share|improve this answer
    
Context is loaded correctly. It works for other sontroller and i tried this but no change in logger output. –  kushal jain Aug 7 '13 at 6:30
    
You have a different a context for testing ? show the error message –  NimChimpsky Aug 7 '13 at 6:34
    
FrameworkServlet '': initialization completed in 1 ms INFO : co.softwarehouse.olt.web.controller.StudentInSectionController - section Id is:1 Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.223 sec <<< FAILURE! testStudentInSectionForm(controller.StudentInSectionControllerTest) Time elapsed: 0.222 sec <<< ERROR! org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException –  kushal jain Aug 7 '13 at 6:45
    
you have missed the interesting bit of the error ... update yr question with more stack trace. How are you setting cotnext for the test ? –  NimChimpsky Aug 7 '13 at 6:59
    
An Xml File is in contextConfiguration. It is working all fine. –  kushal jain Aug 7 '13 at 7:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.