java.lang.NullPointerException: Cannot invoke method value() on null object

Spring JIRA | Wujek | 4 years ago
  1. 0

    I have a simple annotation: {code} @Retention(RetentionPolicy.RUNTIME) @Target([ElementType.TYPE, ElementType.METHOD]) public @interface Tx { String value() default ''; } {code} which I can apply either on a method or class level (which would then mean it applies to all methods). In my aspect, I have the following pointcut and advice: {code} @Pointcut('within(test.service..*) && !execution(* getMetaClass())') void serviceMethods() {} @Around(value = 'serviceMethods() && (@annotation(txmethod) || @target(txclass))', argNames = 'txmethod, txclass') Object aroundService_inconsistentBinding(ProceedingJoinPoint pjp, Tx txmethod, Tx txclass) { println ">>> before $pjp.signature.name with '${txValue(txmethod, txclass)}'" pjp.proceed() } private String txValue(Tx txmethod, Tx txclass) { txmethod ? txmethod.value() : txclass.value() } {code} and a simple usage class: {code} //@Tx('class') class PersonService { @Tx('method') void method() { } } {code} This fails at runtime with the following exception: {quote} Caught: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 inconsistent binding org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 inconsistent binding at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at Main.run(Main.groovy:5) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) Caused by: java.lang.IllegalArgumentException: error at ::0 inconsistent binding at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301) at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:208) at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:194) at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:183) at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:164) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262) at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:359) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:412) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1492) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) ... 12 more {quote} Why is the binding inconsistent? So, I tried to replace the advice with the following: {code} @Around('serviceMethods() && (@annotation(tx) || @target(tx))') Object aroundService_alwaysClassLevelUsed(ProceedingJoinPoint pjp, Tx tx) { println ">>> before '$pjp.signature' with '${tx.value()}'" pjp.proceed() } {code} Now, I get a NPE: {quote} Caught: java.lang.NullPointerException: Cannot invoke method value() on null object java.lang.NullPointerException: Cannot invoke method value() on null object at test.aop.MyAspect.aroundService_alwaysClassLevelUsed(MyAspect.groovy:24) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) at test.service.PersonService$$EnhancerByCGLIB$$aa5c5d10.method(<generated>) at test.service.PersonService$method.call(Unknown Source) at Main.run(Main.groovy:8) {quote} The problem is that the binding expression is not short-circuit, i.e., the annotation on the method is found, but spring (or is it aspectj?) binds further and bind a null Tx annotation on the class level to the parameter. This can be easily checked when the PersonService class has both class-level and method-level annotations configured. I attach a groovy-2.1.0 and gradle-1.4 based project to help easily reproduce this issue. Just invoke {code:none} ./gradlew clean run {code} in the project folder. I also configured remote debugging so, but it doesn't suspend the JVM - change line 78 of build.gradle accordingly.

    Spring JIRA | 4 years ago | Wujek
    java.lang.NullPointerException: Cannot invoke method value() on null object
  2. 0

    I have a simple annotation: {code} @Retention(RetentionPolicy.RUNTIME) @Target([ElementType.TYPE, ElementType.METHOD]) public @interface Tx { String value() default ''; } {code} which I can apply either on a method or class level (which would then mean it applies to all methods). In my aspect, I have the following pointcut and advice: {code} @Pointcut('within(test.service..*) && !execution(* getMetaClass())') void serviceMethods() {} @Around(value = 'serviceMethods() && (@annotation(txmethod) || @target(txclass))', argNames = 'txmethod, txclass') Object aroundService_inconsistentBinding(ProceedingJoinPoint pjp, Tx txmethod, Tx txclass) { println ">>> before $pjp.signature.name with '${txValue(txmethod, txclass)}'" pjp.proceed() } private String txValue(Tx txmethod, Tx txclass) { txmethod ? txmethod.value() : txclass.value() } {code} and a simple usage class: {code} //@Tx('class') class PersonService { @Tx('method') void method() { } } {code} This fails at runtime with the following exception: {quote} Caught: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 inconsistent binding org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 inconsistent binding at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at Main.run(Main.groovy:5) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) Caused by: java.lang.IllegalArgumentException: error at ::0 inconsistent binding at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301) at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:208) at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:194) at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:183) at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:164) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262) at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:359) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:412) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1492) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) ... 12 more {quote} Why is the binding inconsistent? So, I tried to replace the advice with the following: {code} @Around('serviceMethods() && (@annotation(tx) || @target(tx))') Object aroundService_alwaysClassLevelUsed(ProceedingJoinPoint pjp, Tx tx) { println ">>> before '$pjp.signature' with '${tx.value()}'" pjp.proceed() } {code} Now, I get a NPE: {quote} Caught: java.lang.NullPointerException: Cannot invoke method value() on null object java.lang.NullPointerException: Cannot invoke method value() on null object at test.aop.MyAspect.aroundService_alwaysClassLevelUsed(MyAspect.groovy:24) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) at test.service.PersonService$$EnhancerByCGLIB$$aa5c5d10.method(<generated>) at test.service.PersonService$method.call(Unknown Source) at Main.run(Main.groovy:8) {quote} The problem is that the binding expression is not short-circuit, i.e., the annotation on the method is found, but spring (or is it aspectj?) binds further and bind a null Tx annotation on the class level to the parameter. This can be easily checked when the PersonService class has both class-level and method-level annotations configured. I attach a groovy-2.1.0 and gradle-1.4 based project to help easily reproduce this issue. Just invoke {code:none} ./gradlew clean run {code} in the project folder. I also configured remote debugging so, but it doesn't suspend the JVM - change line 78 of build.gradle accordingly.

    Spring JIRA | 4 years ago | Wujek
    java.lang.NullPointerException: Cannot invoke method value() on null object
  3. 0

    NPE when autoreloading a service in Grails 3.2.x

    GitHub | 1 month ago | ilopmar
    java.lang.NullPointerException: Cannot invoke method registerBeans() on null object
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    NullPointerException on selecting tag in repository tags page

    GitHub | 5 months ago | bergtwvd
    java.lang.NullPointerException: Cannot invoke method last() on null object
  6. 0

    Cannot invoke method hasAnnotation() on null object

    Stack Overflow | 1 month ago | Loai
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithRealmTransformerForDebug'.

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.NullPointerException

      Cannot invoke method value() on null object

      at test.aop.MyAspect.aroundService_alwaysClassLevelUsed()
    2. test.aop
      MyAspect.aroundService_alwaysClassLevelUsed
      1. test.aop.MyAspect.aroundService_alwaysClassLevelUsed(MyAspect.groovy:24)
      1 frame
    3. Spring AOP
      CglibAopProxy$DynamicAdvisedInterceptor.intercept
      1. org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
      2. org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
      3. org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
      4. org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
      5. org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
      6. org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      7. org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
      7 frames
    4. test.service
      PersonService$method.call
      1. test.service.PersonService$$EnhancerByCGLIB$$aa5c5d10.method(<generated>)
      2. test.service.PersonService$method.call(Unknown Source)
      2 frames
    5. Unknown
      Main.run
      1. Main.run(Main.groovy:8)
      1 frame