Chandan
Chandan

Reputation: 133

@Around Aspect in Spring AOP throwing Null Pointer Exception

Can someone help me out figuring why my @Around advice throws a Null Pointer Exception here. I have made sure that my advice returns an Object same as the method to proceed, yet I am getting the exception.

@Aspect
public class BasicAuthAspect 
{

    @Around("execution(* *..impl.PreferenceImpl.*(..))") 
    public Object auth(ProceedingJoinPoint joinPoint) {
        log.debug("Inside Basic Auth check method..");
        ServletRequestAttributes requestAttributes=null;
        requestAttributes=(ServletRequestAttributes)
      RequestContextHolder.getRequestAttributes();
        if(requestAttributes!=null){
        request =requestAttributes.getRequest();

         Object[] signatureArgs = joinPoint.getArgs();
            try {
                return (Response) joinPoint.proceed(signatureArgs);
            } catch (Throwable e) {
                return Response.status(HttpStatus.SC_UNAUTHORIZED).build();
            }
    }

        }
        else{
            return Response.status(HttpStatus.SC_UNAUTHORIZED).build();
        }


      }

}

Here is my method to proceed(PreferenceImpl.java):

@Override
    public Response postPreferences( String preference) {


        String responseReturned=searchDAO.postPreference( XSSUtils.normalize(preference));
        if(responseReturned!=null)
        response=Response.status("success").build();
        else
        response=Response.status("failure").build();
        return response;    
        }

Here goes the error log:

java.lang.NullPointerException
    at java.lang.String.<init>(String.java:556)[:1.7.0_80]
    at org.apache.cxf.transport.http.AbstractHTTPDestination.getAuthorizationPolicyFromMessage(AbstractHTTPDestination.java:163)[173:org.apache.cxf.cxf-rt-transports-http:2.7.3]
    at org.apache.cxf.transport.http.AbstractHTTPDestination.setupMessage(AbstractHTTPDestination.java:349)[173:org.apache.cxf.cxf-rt-transports-http:2.7.3]
    at org.apache.cxf.transport.http.AbstractHTTPDestination.setupMessage(AbstractHTTPDestination.java:258)[173:org.apache.cxf.cxf-rt-transports-http:2.7.3]
    at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.serviceRequest(JettyHTTPDestination.java:345)[179:org.apache.cxf.cxf-rt-transports-http-jetty:2.7.3]
    at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:319)[179:org.apache.cxf.cxf-rt-transports-http-jetty:2.7.3]
    at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:72)[179:org.apache.cxf.cxf-rt-transports-http-jetty:2.7.3]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1040)[79:org.eclipse.jetty.server:7.6.8.v20121106]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:976)[79:org.eclipse.jetty.server:7.6.8.v20121106]

Upvotes: 1

Views: 3755

Answers (1)

kriegaex
kriegaex

Reputation: 67297

You could have several problems here:

  • Your pointcut intercepts all PreferenceImpl methods, but it seems you only want to catch a single one. I suggest you make the pointcut more specific.

  • You are assuming return type Response in the advice method, but your pointcut assumes * and the advice method itself returns Object, so theoretically another method could return another type and the cast would fail (see first problem).

  • You are trying to provide arguments to proceed(), but that is actually not necessary. So just remove the getArgs() stuff, you do not need it.

If despite my hints you still have problems, let me know and I can provide some sample code. BTW, maybe you want to read a basic Spring AOP tutorial. :-)

Upvotes: 1

Related Questions