1

My DataNotFoundException is this:

public class DataNotFoundException extends RuntimeException
{
    private static final long serialVersionUID = 240618041975267964L;

    public DataNotFoundException( String message )
    {
        super( message );
    }
}

and i perfectly mapped it with ExceptionMapper

@Provider public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException> { @Override public Response toResponse( DataNotFoundException e ) { ErrorMessage errorMessage = new ErrorMessage( e.getMessage(), 404, "http://www.google.com" ); return Response.status( Response.Status.NOT_FOUND ).entity( errorMessage ).build(); } }

When this exception is thrown, it gives me internal error which means it doesn't mapped.

'<pre>java.lang.NullPointerException
org.srdc.arda.playground.service.MessageService.getMessage(MessageService.java:26)
org.srdc.arda.playground.resources.MessageResource.getMessage(MessageResource.java:37)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

'

but if i map every exception with something like this:

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable>
{
    @Override
    public Response toResponse( Throwable throwable )
    {
        ErrorMessage errorMessage = new ErrorMessage( throwable.getMessage(), 500, "www.hotmail.com" );
        return Response.status( Response.Status.INTERNAL_SERVER_ERROR ).entity( errorMessage ).build();
    }
}

this works fine.

I throw the exception through this method:

    public Message getMessage( long i )
{
    Message message = messages.get( i );
    if ( message == null )
    {
        throw new DataNotFoundException( "Message with id " + message.getId() + " not found." );
    }
    return message;
}

What may be the reason behind this?

Thanks in advance.

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
Arda Güney
  • 135
  • 1
  • 2
  • 7
  • 1
    Did you look at the stack trace? `NullPointerException` isn't a `DataNotFoundException`. You need to figure out why `MessageService.getMessage` is causing the NPE at line 26 – Paul Samsotha Nov 27 '15 at 12:30
  • @peeskillet I added the method that throws exception. I do think it should throw DataNotFoundException. It works perfectly fine for reachable messages. – Arda Güney Nov 27 '15 at 12:39
  • 1
    Check if `messsages` is null. I'm sure it is. That's what's causing the NPE. `DataNotFoundException` is never thrown as that line is never reached – Paul Samsotha Nov 27 '15 at 12:40
  • Actually I take that back. How can you call `message.getId()` if `message` is null? You probably want to use `i` instead. – Paul Samsotha Nov 27 '15 at 12:48
  • Thank you very much @peeskillet . That was the problem. – Arda Güney Nov 27 '15 at 12:52

0 Answers0