Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract char

Tags:

java

jpa

I have a table defined with column char and the repo I have defined a query to return that column. Now when the data in the db table is null for the specific condition its giving me the error:

org.springframework.aop.AopInvocationException: Null return value from 
advice does not match primitive return type for: public abstract char

Below is the code snippet:

    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;

    import com.marriott.sup.beans.StepWorkflow;

    public interface flowRepo extends JpaRepository<StepWorkflow, Long> {

        @Query(
                value = "SELECT s.status FROM flow s WHERE s.XREFID 
     = :xrefId order by "
                + "s.Datestarted desc limit 1",
                nativeQuery = true)
        public char findLatestSts(@Param("xrefId") Long xrefId);

    }

    public class flowService {

        public static char INPROGRESS = 'P';
        public static char SUCCESS = 'S';
        public static char ERROR = 'E';
        public static char NOTSTARTED = 'N';

        @Autowired
        private flowRepo Fl;

        /*some basic code in between and then i am trying to call the 
      function 
      defined in repo and assign it to a character variable
    */

    Sts = Fl.findLatestSts(xrefId);
    }

I need this to not throw the error and instead assign some default value in case null. Need help to fix the issue on how can I handle this.

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract char org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:226) at com.sun.proxy.$Proxy108.findLatestSts(Unknown Source) at com.marriott.sup.service.StepWorkflowService.getserverStepstatus(StepWorkflowService.java:79) at com.marriott.sup.web.SampleRestController.getServerStepStatus(SampleRestController.java:91) 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:189) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
like image 349
Daya Avatar asked Jun 07 '19 16:06

Daya


1 Answers

Try to use Character instead of char. There is situation when your query return NULL and as you know primitive types in Java can not save Null inside. So, use Object of that type like int -> Integer, float -> Float, char -> Character.

Or, If you really need to work with primitive, put your method inside try-catch block, to handle that issue.

like image 128
Dred Avatar answered Sep 18 '22 01:09

Dred