2013-06-27 60 views
0

我想填充在Spring MVC一個下拉框,我試圖這樣做,但我得到空指針異常如何獲得動態下拉框在JSP

這是我的控制器:

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

這是我服務的方法:

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

這是我的DAO方法:

@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(); 
} 

終於這是我的JSP頁面:

<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> 

這是堆棧跟蹤:

顯示java.lang.NullPointerException 在com.easylib.elibrary.webapp.controller.catalogue.CatalogueController.newMember(CatalogueController的.java:80) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在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) 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) 在org.springframework.web.servlet.mvc.method。 annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) 在org.springframework.web.servlet.mvc。 method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodA dapter.java:80) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)

回答

0

你所得到的NPE在控制器的80線,但我看不出哪行這是你的問題。

我的猜測是,你有沒有在你的服務的有線正確,要麼「publisherService」或「subjectService」爲空。

+0

line no 80 is this:List subjectlist = subjectService.getSubjectList(); –

+0

在這種情況下,唯一的選擇就是subjectService尚未初始化,你調用一個空對象的方法。 – ajames

+0

should'nt private SubjectService subjectService爲@Autowired private SubjectService subjectService,然後調用subjectService。 getSubjectList() – sreeprasad