2013-05-03 109 views
0

我的方法一直返回一個Null異常。我想從數據庫中檢索一些對象。Spring - java.lang.NullPointerException

所討論的方法

@Transactional 
@SuppressWarnings("unchecked") 
public UserEntity getWithModules(Integer id){ 

    Criteria crit = getCurrentSession().createCriteria(UserEntity.class); 
    crit.add(Restrictions.eq("id", id)); 
    UserEntity userEntity = get(crit); 

    //Retrieve the modules for the staff 
    crit = getCurrentSession().createCriteria(Module.class); 

    crit.add(Restrictions.eq("id", id)); 

    crit.addOrder(Order.asc("moduleId")); 
    Set<Module> sModule = new LinkedHashSet<Module>(crit.list()); 
    userEntity.setsModule(sModule); 

    return userEntity; 
} 

控制器,用於它

@RequestMapping(value="/home/{id}", method = 

    RequestMethod.GET) 
    public String showStaffModules(@PathVariable("id") Integer id, ModelMap map, 

    HttpServletRequest request) { 

      map.addAttribute("cp", request.getContextPath()); 

      map.addAttribute("Setter", userService.getWithModules(id)); 

      return "/home"; 

堆棧跟蹤

 java.lang.NullPointerException 
com.myproject.app.dao.UserDAO.getWithModules(UserDAO.java:59) 
com.myproject.app.dao.UserDAO$$FastClassByCGLIB$$20f5ced0.invoke(<generated>) 
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) 
com.myproject.app.dao.UserDAO$$EnhancerByCGLIB$$a44426b2.getWithModules(<generated>) 
com.myproject.app.service.UserServiceImpl.getWithModules(UserServiceImpl.java:58) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:601) 
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
$Proxy133.getWithModules(Unknown Source) 
com.myproject.app.controller.UserController.getSetterPage(UserController.java:112) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:601) 
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746) 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687) 
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) 
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.access.channel.ChannelProcessingFilter.doFilter(ChannelProcessingFilter.java:144) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 

尋找在我上線59檢查堆棧跟蹤是從上述方法,這代碼:

 userEntity.setsModule(sModule); 

我想知道爲什麼無法檢索對象。

+0

'userEntity'爲'null'。 – Zenexer 2013-05-03 20:06:14

+0

是的,我已經檢查過,它確實返回null。它不應該像ID一樣在數據庫中。 – user2259555 2013-05-04 00:13:31

回答

0

沒有一個UserEntity與您指定的id,所以get(crit)回報null。當您嘗試調用userEntity.setsModule時,userEntitynull

您需要檢查get(crit)返回的null。這表明您的標準沒有匹配。

+0

感謝您解釋它。我沒有檢查,它確實返回null。返回的查詢說ID是空的。我真的不知道爲什麼用戶在數據庫中。有什麼方法可以檢索細節嗎? – user2259555 2013-05-04 00:12:22

+0

「id」是該字段的實際名稱嗎?案件事宜。您可能還需要檢查系統的狀態以確保它已準備好進行查詢,並查詢您期望的相同數據集。您也可以不受任何限制地檢索所有條目,並對一些結果進行抽樣以查看它們是否與您預期的相同。 – Zenexer 2013-05-04 00:17:56

+0

是的id是該字段的名稱。我已經列出了網頁上的所有模塊,以查看它是否可以。數據集如預期的那樣。我也列出了所有的用戶,並可以從數據庫中檢索他們的詳細信息。我似乎無法打開一個頁面,其中列出了特定於該用戶的所有子項。 – user2259555 2013-05-04 00:48:08

0

無論你正在做的get()方法可能會返回一個空引用:

UserEntity userEntity = get(crit); 
+0

是的,這是正確的。添加空檢查後,該對象返回null。 – user2259555 2013-05-04 00:14:22

相關問題