0
votes

I want to populate a dropdown box in Spring MVC i tried to do this but i am getting null pointer exception

this is my controller:

public class CatalogueController  {
private CatalogueService catalogueService;
private PublisherService publisherService;
private SubjectService subjectService; 

   // some code and i generated setter and getter methods  
   ............

 @RequestMapping(value="/catalogue/new.action", method=RequestMethod.GET)  
   public ModelAndView newMember() throws Exception {
    ModelAndView mvc= null;
    mvc =   new ModelAndView("catalogue/catalogueForm", "catalogueForm", new CatalogueBase());
    mvc.addObject("copyDetailForm", new CatalogueCopyDetails());

    List<Publisher> publist = publisherService.getPublisherList();
    mvc.addObject("publist", publist);

    List<Subject> subjectlist = subjectService.getSubjectList();
    mvc.addObject("subjectlist", subjectlist);

    return mvc;
}  

this is my service method :

    @Override
public List<Publisher> getPublisherList() {
    List<Publisher> list = publisherDAO.getPublisher();
    return list;
}
    @Override
public List<Subject> getSubjectList() {
    List<Subject> list = subjectDAO.getSubjects();
    return list;
}

this is my DAO Method:

    @SuppressWarnings("unchecked")
public List<Publisher> getPublisher() {
      Query qry = getSession().createQuery("from Publisher");
        return qry.list();
}
    @SuppressWarnings("unchecked")
public List<Subject> getSubjects() {
      Query qry = getSession().createQuery("from Subject");
        return qry.list();
}

finally this is my JSP page :

  <form:form commandname="catalogueForm"    action="${pageContext.request.contextPath}/catalogue/create.action" method="post" modelAttribute="catalogueForm">

    <form:select path="publisher.id" id="publisher.id">
    <form:options items="${publist}" itemValue="id" itemLabel="name" />
    </form:select>                                                        

    <form:select path="subject.id" id="subject.id">                            
    <form:options items="${subjectlist}" itemValue="id" itemLabel="name" />
    </form:select>

   </form:form>

This is Stack Trace:

java.lang.NullPointerException at com.easylib.elibrary.webapp.controller.catalogue.CatalogueController.newMember(CatalogueController.java:80) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)

1

1 Answers

0
votes

You are getting the npe at line 80 of your controller, but I can't see which line that is in your question.

My guess is that you haven't wired in your services correctly and either "publisherService" or "subjectService" is null.