0
votes

Here is the Entity's:

public class ProductOffer extends AuditModel implements Serializable {

    @Id
    @GeneratedValue(strategy =  GenerationType.IDENTITY)
    private Integer id = null;
    
    @Column(name = "offer_code", nullable = false)
    private String offerCode = null;

    @NotNull
    @Column(unique = true)
    private String version = null;

    @Column
    private String offerCodeVersion = null;

    @Column
    private String offerName = null;

    @Column
    private String description = null;

    @Column
    private String status = null;

    @JsonProperty
    private OperationEnum operation;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "lifecycleStatus_id", referencedColumnName = "lId")
    @JsonIgnore
    private LifeCycleStatus lifecycleStatus = null;
}

public class LifeCycleStatus extends AuditModel implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer lId = null;

    @Column
    private String name = null;
    
    @OneToMany(mappedBy = "lifecycleStatus", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<ProductOffer> productOffer = new ArrayList<ProductOffer>();
}

Herer is the DAO:

public void addProductOffer(ProductOffer productOffer) {

        Session session = entityManager.unwrap(Session.class);
        LifeCycleStatus lifeCycleStatus = lifeCycleStatusDAO.findById(productOffer.getLifecycleStatus().getLId()); /* am getting NPE here */
        if(lifeCycleStatus == null)
            lifeCycleStatus = new LifeCycleStatus();
        lifeCycleStatus.setName(productOffer.getLifecycleStatus().getName());
        productOffer.setLifecycleStatus(lifeCycleStatus);
        session.saveOrUpdate(productOffer);
    }

Here is the Post mapping details:

{

   "offerCode":"HG123",
   "version":"2",
   "offerCodeVersion":"2.2",
   "offerName":"Recharge",
   "description":"aadhakjdhaks",
   "status":"Inactive",
   "operation":"PUBLISH",
   "lifecycleStatus":{
       "lId":2,
       "name":"InActive"
   }
}

Here is the complete error log

2020-07-21 18:55:42 - o.a.c.c.C.[.[.[.[dispatcherServlet] -

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null at com.hima.demo.dao.ProductOfferDAO.addProductOffer(ProductOfferDAO.java:34) at com.hima.demo.dao.ProductOfferDAO$$FastClassBySpringCGLIB$$2cf28c6a.invoke() at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) at com.hima.demo.dao.ProductOfferDAO$$EnhancerBySpringCGLIB$$52838a30.addProductOffer() at com.hima.demo.services.ProductOfferService.addProductOffer(ProductOfferService.java:26) at com.hima.demo.services.ProductOfferService$$FastClassBySpringCGLIB$$92d2ad77.invoke() at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) at com.hima.demo.services.ProductOfferService$$EnhancerBySpringCGLIB$$3ba67d77.addProductOffer() at com.hima.demo.controller.ProductOfferController.createProductOffer(ProductOfferController.java:35) 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:190) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) 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)

Can any one please help me on this?

I have referred to many articles but no use please help me. I am not getting an idea where am missing..

1
can you provide some way to recreate the error,dalusC
What are you trying to achieve with this line @JsonIgnore private LifeCycleStatus lifecycleStatus = null; ?Amit kumar
Which line is line 34 in DAO ?Eklavya
Here is the line 34: LifeCycleStatus lifeCycleStatus = lifeCycleStatusDAO.findById(productOffer.getLifecycleStatus().getLId()); /* am getting NPE here */Hima
i.e ManyToOne Mapping @JsonIgnore private LifeCycleStatus lifecycleStatus = null;--> We have different status of the producoffer. so trying to mapping with status and update the status u may look into LifeCycleStatus Entity class. this is bi-directional mappingHima

1 Answers

0
votes

I know it's silly, but can you rename lId to lid (capital 'I' to small 'i')??

variable name lid with all small latter and do relevant changes in all other files as well in DAO and then try again!

also from postman send request with "lid" (all small latter)