【发布时间】:2017-08-24 16:32:30
【问题描述】:
我面临的问题是,当客户尝试添加商品到购物车时,控制器在这行代码中会引发 NullPointerException
CustomerCart customercart = customer.getCustomercart();
请查看异常信息
xps_id:XPS00008 数量:1 CUD00001 2017 年 4 月 2 日下午 12:39:10 org.apache.catalina.core.StandardWrapperValve 调用严重: Servlet.service() 用于带有路径的上下文中的 servlet [调度程序] [/TechNXT] 抛出异常 [请求处理失败;嵌套的 异常是 java.lang.NullPointerException] 的根本原因 java.lang.NullPointerException 在 technxt.controller.TechNXTCustomerController.addItemToCart(TechNXTCustomerController.java:150) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(未知来源)在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 在 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) 在 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) 在 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 在 org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) 在 org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) 在 org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 在 org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) 在 org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) 在 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495) 在 org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767) 在 org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1354) 在 org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(未知来源) 在 org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 在 java.lang.Thread.run(Unknown Source)
所以 addItemToCart 是我的控制器方法 这是方法
@RequestMapping("/customerreqAddItemToCart/{xps_id}")
public String addItemToCart(@PathVariable("xps_id")String xps_id,@RequestParam("qty")int qty,Principal principal,HttpSession hsession,Model m){
System.out.println("xps_id : "+xps_id + " qty : " + qty);
String customerid=principal.getName();
System.out.println(customerid);
Customer customer =cuss.getCustomerByUserId(customerid);
CustomerCart customercart = customer.getCustomercart();
Xmap_ps xmap_ps = xpss.getXPSbyId(xps_id);
CustomerCartItems cartitem = new CustomerCartItems();
cartitem.setCustomercart(customercart);
cartitem.setXmap_ps(xmap_ps);
cartitem.setCus_cartquantity(qty);
cartitem.setCus_cartitemwisetotal(xmap_ps.getXps_price()*qty);
/*cartitem.setProduct_id(xmap_ps.getProduct_id());
cartitem.setSupplier_id(xmap_ps.getSupplier_id());*/
ccis.addCartItem(cartitem);
Customer customer1=cuss.getCustomerByUserId(customerid);
CustomerCart cart1=customer1.getCustomercart();
int cartsize = ccss.getCartSize(cart1);
List<CustomerCartItems> cartitems = cart1.getCustomercartitems();
int sum=0;
for(CustomerCartItems critem :cartitems){
sum+= critem.getCus_cartitemwisetotal();
}
cart1.setCus_cartgrandtotal(sum);
ccss.updateCart(cart1);
List<VwXmapPS> xpsdata = vxpss.getBestVwPs();
m.addAttribute("xpsdata", xpsdata);
m.addAttribute("customername",hsession.getAttribute("customername"));
m.addAttribute("customerid",hsession.getAttribute("customerid"));
m.addAttribute("cartsize",hsession.getAttribute("cartsize"));
m.addAttribute("cartmessage","Item added to cart successfully");
hsession.setAttribute("cartsize", cartsize);
System.out.println("cart size :" + cartsize);
return "redirect:/reqdisplaycustomerhomepage";
}
让我也与大家分享我的模型课
- CustomerCart 与 CustomerCartItems 模型类具有一对多关系的模型类
@Entity
public class CustomerCart {
@Id
private String cus_cartid;
private double cus_cartgrandtotal;
@OneToOne(mappedBy="customercart")
private Customer customer;
@OneToMany(mappedBy="customercart", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<CustomerCartItems> customercartitems;
- CustomerCartItems 模型类
@Entity
public class CustomerCartItems {
@Id
private String cus_cartitemid;
private int cus_cartquantity;
private int cus_cartitemwisetotal;
@ManyToOne
@JoinColumn(name="cus_cartid")
private CustomerCart customercart;
@ManyToOne
@JoinColumn(name="xps_id")
private Xmap_ps xmap_ps;
3.还有 Customer 模型类,它与其他表具有所有外键关系
@Entity
public class Customer {
@Id
private String cus_id;
private String cus_name;
private String cus_emailid;
private String cus_mobileno;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="cus_loginid")
private CustomerDetails customerdetails;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="cus_billingaddressid")
private CustomerBillingAddress customerbillingaddress;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="cus_shippingaddressid")
private CustomerShippingAddress customershippingaddress;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="cus_cartid")
private CustomerCart customercart;
我认为我的模型类没有犯任何错误 请让我知道我还有什么可以与大家分享的,以便我可以找到解决方案
感谢您抽出宝贵时间帮助我
【问题讨论】:
-
对不起,我根据我看到的一半代码给出了响应。如果空指针出现在您在顶部显示的行中,则与依赖注入无关。更多看起来像`customer=cuss.getCustomerByUserId(customerid);`给你空客户,它在下一行产生空指针。检查数据库,看看您是否能够找到 customerid 的客户记录。
-
@VijendraKulhade 先生,我没有将 Customerid 设为 null .. 这是我用来打印 customerid
psid : XPS00008 qty : 1 CUD00001的控制台结果 -
@AyushAgarwal 虽然
customerid不为空,但正如 Vijendra Kulhade 所说,您很可能在 db 中没有任何带有该 ID 的Customer。这就是为什么customer=cuss.getCustomerByUserId(customerid);将customer设置为 null 并从您调用customer.getCustomercart();的地方抛出 NPE -
@minarmahmud 先生,客户 ID 在数据库中可用!
标签: java spring hibernate spring-mvc spring-security