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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With