Spring MVC - 获取模型的 NullPointerException Class

Spring MVC - Getting NullPointerException of a Model Class

我面临的问题是,当客户尝试将商品添加到购物车时,在 Controller 处引发 NullPointerException这行代码

CustomerCart customercart = customer.getCustomercart();

请检查异常信息

xps_id : XPS00008 qty : 1 CUD00001 Apr 02, 2017 12:39:10 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/TechNXT] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at technxt.controller.TechNXTCustomerController.addItemToCart(TechNXTCustomerController.java:150) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1354) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at 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";


    }

让我也与大家分享我的模型class

  1. CustomerCart Model class which has One to Many Relationship with CustomerCartItems Model class
@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;
  1. CustomerCartItems Model class
@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.And also The Customer Model Class which has all the Foreign Key relationship to other tables

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

我认为我的模型没有任何错误 Class 请让我知道我还能与大家分享什么,以便我能找到解决方案

感谢您花时间帮助我 <3

这真的如您所想的那样吗?:

String customerid=principal.getName();

它认为它不会为您提供数据库中的 Id 字段值。 您可以将登录用户的 ID 存储在会话中,或者将其作为 jsp.

的请求参数

对其进行调试,您将看到 customer 为空。