Null Pointer on exception throwing


#1

Hello, I’m working on Kotlin 1.2.21 and Spring Boot 1.5.9

I have a small exception class

class MyLabException constructor(val error: MLErrors) : RuntimeException() {

    constructor(error: MLErrors, cause: Throwable) : this(error) {
        this.initCause(cause)
    }

}

when I call this snippet of code on the last operation (the throw of the exception) the JVM goes to a NPE

val user = userRepository.findByEmail(email) ?: throw MyLabException(MLErrors.US_EMAIL_OR_PASSWORD_WRONG)

Note: MLErrors is just an Enum class.

This is the stack trace. Can you give me any advice? I’m going crazy…

com.example.commonsapi.exception.MyLabException: null
	at com.example.elementsapi.service.UserService.login(UserService.kt:70) ~[classes/:na]
	at com.example.elementsapi.service.UserService$$FastClassBySpringCGLIB$$7fad5cc0.invoke(<generated>) ~[classes/:na]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at com.example.elementsapi.service.UserService$$EnhancerBySpringCGLIB$$a9d72cf6.login(<generated>) ~[classes/:na]
	at com.example.elementsapi.controller.SessionController.create(SessionController.kt:30) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]

#2

This is not an NPE, this is the exact exception you’re throwing. Since you’re calling the default constructor of the base class (RuntimeException), the message of your exception is null.


#3

Sorry, deep night coding after a day of work is never a good idea :slight_smile: i figured it out after a coffee!