jersey
  1. jersey
  2. JERSEY-1887

NPE with intermediate ExceptionMapper interface

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.17, 2.3.1
    • Fix Version/s: 2.5
    • Component/s: core
    • Labels:
      None

      Description

      I get the following stacktrace with jersey 1.17.1:

      java.lang.NullPointerException
      	at com.sun.jersey.core.spi.component.ComponentInjector.inject(ComponentInjector.java:87)
      	at com.sun.jersey.core.spi.component.ioc.IoCProviderFactory$ManagedSingleton.<init>(IoCProviderFactory.java:180)
      	at com.sun.jersey.core.spi.component.ioc.IoCProviderFactory.wrap(IoCProviderFactory.java:100)
      	at com.sun.jersey.core.spi.component.ioc.IoCProviderFactory._getComponentProvider(IoCProviderFactory.java:93)
      	at com.sun.jersey.core.spi.component.ProviderFactory.getComponentProvider(ProviderFactory.java:153)
      	at com.sun.jersey.core.spi.component.ProviderServices.getComponent(ProviderServices.java:251)
      	at com.sun.jersey.core.spi.component.ProviderServices.getProviders(ProviderServices.java:148)
      	at com.sun.jersey.server.impl.application.ExceptionMapperFactory.init(ExceptionMapperFactory.java:74)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1307)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770)
      	at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770)
      	at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
      	at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
      	at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
      	at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
      	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
      	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
      	at javax.servlet.GenericServlet.init(GenericServlet.java:244)
      	at org.glassfish.grizzly.servlet.WebappContext.initServlets(WebappContext.java:1477)
      	at org.glassfish.grizzly.servlet.WebappContext.deploy(WebappContext.java:265)
      ...
      

      With the following test:

      @ContextConfiguration(classes = MyTest.MyConfiguration.class)
      public class MyTest extends JerseySpringTest /* this superclass is one of my own, not currently open sourced */ {
      	@Test
      	public void test() {
      		System.out.println(resource().path("/").get(String.class));
      	}
      
      	@Configuration
      	public static class MyConfiguration {
      		@Bean
      		public MyResource myResource() {
      			return new MyResource();
      		}
      
      		@Bean
      		@Scope(value = SCOPE_SINGLETON)
      		public MyExceptionMapper myExceptionMapper() {
      			return new MyExceptionMapperImpl();
      		}
      	}
      
      	@Provider 
      	public interface MyExceptionMapper extends ExceptionMapper<Throwable> {
      	}
      
      	public static class MyExceptionMapperImpl implements MyExceptionMapper {
      		@Override
      		public Response toResponse(Throwable exception) {
      			return Response.status(Response.Status.FORBIDDEN).build();
      		}
      	}
      
      	@Path("/")
      	public static class MyResource {
      		@GET
      		public String get() {
      			throw new RuntimeException();
      		}
      	}
      }
      

        Activity

        Hide
        Mikhail Mazursky added a comment - - edited

        I'm getting similar exception:

        09:12:28.282 [Grizzly(4)][] ERROR ServletHandler:260 - service exception:
        javax.servlet.ServletException: java.lang.NullPointerException
        	at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:139) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:252) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:188) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:220) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:175) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:273) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:818) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:562) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:542) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
        Caused by: java.lang.NullPointerException: null
        	at java.lang.Class.isAssignableFrom(Native Method) ~[na:1.7.0_25]
        	at com.sun.jersey.server.impl.application.ExceptionMapperFactory.getExceptionType(ExceptionMapperFactory.java:115) ~[jersey-server-1.17.1.jar:1.17.1]
        	at com.sun.jersey.server.impl.application.ExceptionMapperFactory.init(ExceptionMapperFactory.java:75) ~[jersey-server-1.17.1.jar:1.17.1]
        	at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1307) ~[jersey-server-1.17.1.jar:1.17.1]
        	at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168) ~[jersey-server-1.17.1.jar:1.17.1]
        	at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774) ~[jersey-server-1.17.1.jar:1.17.1]
        	at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770) ~[jersey-server-1.17.1.jar:1.17.1]
        	at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) ~[jersey-core-1.17.1.jar:1.17.1]
        	at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770) ~[jersey-server-1.17.1.jar:1.17.1]
        	at com.sun.jersey.guice.spi.container.servlet.GuiceContainer.initiate(GuiceContainer.java:121) ~[jersey-guice-1.17.1.jar:1.17.1]
        	at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319) ~[jersey-servlet-1.17.1.jar:1.17.1]
        	at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605) ~[jersey-servlet-1.17.1.jar:1.17.1]
        	at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210) ~[jersey-servlet-1.17.1.jar:1.17.1]
        	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374) ~[jersey-servlet-1.17.1.jar:1.17.1]
        	at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557) ~[jersey-servlet-1.17.1.jar:1.17.1]
        	at javax.servlet.GenericServlet.init(GenericServlet.java:244) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	at com.google.inject.servlet.ServletDefinition.init(ServletDefinition.java:117) ~[guice-servlet-3.0.jar:na]
        	at com.google.inject.servlet.ManagedServletPipeline.init(ManagedServletPipeline.java:82) ~[guice-servlet-3.0.jar:na]
        	at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:102) ~[guice-servlet-3.0.jar:na]
        	at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:114) ~[guice-servlet-3.0.jar:na]
        	at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) ~[guice-servlet-3.0.jar:na]
        	at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:137) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2]
        	... 21 common frames omitted
        

        The cause of this is the same - I added intermediate interface that extends ExceptionMapper. The difference is that my interface is parametrized like this:

        interface StatusExceptionMapper<E extends Throwable> extends ExceptionMapper<E>
        

        Looking at the code of ExceptionMapperFactory.getType()

            private Class getType(Class<? extends ExceptionMapper> c) {
                Class _c = c;
                while (_c != Object.class) {
                    Type[] ts = _c.getGenericInterfaces();
                    for (Type t : ts) {
                        if (t instanceof ParameterizedType) {
                            ParameterizedType pt = (ParameterizedType)t;
                            if (pt.getRawType() == ExceptionMapper.class) {
                                return getResolvedType(pt.getActualTypeArguments()[0], c, _c);
                            }
                        }
                    }
                    
                    _c = _c.getSuperclass();
                }
                
                // This statement will never be reached
                return null;        
            }
        

        we see that the problem is that "t" is a StatusExceptionMapper and "if", that checks if types are same, fails.

        Also, instead of returning null it's probably better to throw AssertionError.

        Show
        Mikhail Mazursky added a comment - - edited I'm getting similar exception: 09:12:28.282 [Grizzly(4)][] ERROR ServletHandler:260 - service exception: javax.servlet.ServletException: java.lang.NullPointerException at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:139) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:252) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:188) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:220) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:175) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:273) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:818) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:562) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:542) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25] Caused by: java.lang.NullPointerException: null at java.lang.Class.isAssignableFrom(Native Method) ~[na:1.7.0_25] at com.sun.jersey.server.impl.application.ExceptionMapperFactory.getExceptionType(ExceptionMapperFactory.java:115) ~[jersey-server-1.17.1.jar:1.17.1] at com.sun.jersey.server.impl.application.ExceptionMapperFactory.init(ExceptionMapperFactory.java:75) ~[jersey-server-1.17.1.jar:1.17.1] at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1307) ~[jersey-server-1.17.1.jar:1.17.1] at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168) ~[jersey-server-1.17.1.jar:1.17.1] at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774) ~[jersey-server-1.17.1.jar:1.17.1] at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770) ~[jersey-server-1.17.1.jar:1.17.1] at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) ~[jersey-core-1.17.1.jar:1.17.1] at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770) ~[jersey-server-1.17.1.jar:1.17.1] at com.sun.jersey.guice.spi.container.servlet.GuiceContainer.initiate(GuiceContainer.java:121) ~[jersey-guice-1.17.1.jar:1.17.1] at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319) ~[jersey-servlet-1.17.1.jar:1.17.1] at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605) ~[jersey-servlet-1.17.1.jar:1.17.1] at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210) ~[jersey-servlet-1.17.1.jar:1.17.1] at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374) ~[jersey-servlet-1.17.1.jar:1.17.1] at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557) ~[jersey-servlet-1.17.1.jar:1.17.1] at javax.servlet.GenericServlet.init(GenericServlet.java:244) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] at com.google.inject.servlet.ServletDefinition.init(ServletDefinition.java:117) ~[guice-servlet-3.0.jar:na] at com.google.inject.servlet.ManagedServletPipeline.init(ManagedServletPipeline.java:82) ~[guice-servlet-3.0.jar:na] at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:102) ~[guice-servlet-3.0.jar:na] at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:114) ~[guice-servlet-3.0.jar:na] at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) ~[guice-servlet-3.0.jar:na] at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:137) ~[grizzly-http-servlet-server-2.3-rc2.jar:2.3-rc2] ... 21 common frames omitted The cause of this is the same - I added intermediate interface that extends ExceptionMapper. The difference is that my interface is parametrized like this: interface StatusExceptionMapper<E extends Throwable> extends ExceptionMapper<E> Looking at the code of ExceptionMapperFactory.getType() private Class getType( Class <? extends ExceptionMapper> c) { Class _c = c; while (_c != Object .class) { Type[] ts = _c.getGenericInterfaces(); for (Type t : ts) { if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType)t; if (pt.getRawType() == ExceptionMapper.class) { return getResolvedType(pt.getActualTypeArguments()[0], c, _c); } } } _c = _c.getSuperclass(); } // This statement will never be reached return null ; } we see that the problem is that "t" is a StatusExceptionMapper and "if", that checks if types are same, fails. Also, instead of returning null it's probably better to throw AssertionError.
        Hide
        Michal Gajdos added a comment -

        Targeting also for 2.x.

        Show
        Michal Gajdos added a comment - Targeting also for 2.x.

          People

          • Assignee:
            Michal Gajdos
            Reporter:
            hertzsprung
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 30 minutes Original Estimate - 30 minutes
              30m
              Remaining:
              Remaining Estimate - 0 minutes
              0m
              Logged:
              Time Spent - 2 hours
              2h