首页

通过method.getAnnotation(SysLog.class)获取方法的注解对象时报java.lang.NullPointerException空指针异常

标签:NullPointerException,注解     发布时间:2022-08-17   

一、异常说明

通过方法反射获取method.getAnnotation(SysLog.class)注解对象时,报空指针java.lang.NullPointerException: null

java.lang.NullPointerException: null@b@	at com.nacos.xwood.log.aop.SysLogAop.getMethodDescription(SysLogAop.java:93) ~[classes/:na]@b@	at com.nacos.xwood.log.aop.SysLogAop.doAround(SysLogAop.java:70) ~[classes/:na]@b@	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]@b@	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]@b@	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]@b@	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]@b@	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.6.jar:5.3.6]@b@	at com.nacos.engineering.controller.ChannelWhiteController$$EnhancerBySpringCGLIB$$85ffbbc7.getWhiteList(<generated>) ~[classes/:na]@b@	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]@b@	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]@b@	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]@b@	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]@b@	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.6.jar:5.3.6]@b@	at com.ruicar.afs.cloud.common.core.tangram.enditpoint.TangramInvocableHandlerMethod.invokeAndHandle(TangramInvocableHandlerMethod.java:79) ~[afs-common-core-1.1.2-20220524.140130-2.jar:na]@b@	at com.ruicar.afs.cloud.common.core.tangram.enditpoint.TangramMethodHandlerAdapter.invokeHandlerMethod(TangramMethodHandlerAdapter.java:829) ~[afs-common-core-1.1.2-20220524.140130-2.jar:na]@b@	at com.ruicar.afs.cloud.common.core.tangram.enditpoint.TangramMethodHandlerAdapter.handleInternal(TangramMethodHandlerAdapter.java:743) ~[afs-common-core-1.1.2-20220524.140130-2.jar:na]@b@	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.6.jar:5.3.6]@b@	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) ~[spring-webmvc-5.3.6.jar:5.3.6]@b@	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) ~[spring-webmvc-5.3.6.jar:5.3.6]@b@	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.6.jar:5.3.6]@b@	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.6.jar:5.3.6]@b@	at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.45.jar:4.0.FR]@b@	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.6.jar:5.3.6]@b@	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.45.jar:4.0.FR]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:204) ~[spring-security-web-5.4.6.jar:5.4.6]@b@	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:183) ~[spring-security-web-5.4.6.jar:5.4.6]@b@	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93) ~[spring-boot-actuator-2.4.5.jar:2.4.5]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_221]@b@	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]@b@	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.45.jar:9.0.45]@b@	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]

自定义注解com.nacos.xwood.log.annotation.SysLog

import java.lang.annotation.*;@b@@b@/**@b@ *   定义操作日志注解申明@b@ *  * @author jun.ni  @b@ *  * @version 1.0.0  @b@ *  * @ClassName SysLog.java  @b@ *  * @Description TODO  @b@ *  * @createTime  2022/8/15 16:39  @b@ *  @b@ */@b@@Target({ElementType.PARAMETER, ElementType.METHOD})@b@@Retention(RetentionPolicy.RUNTIME)@b@@Documented@b@public @interface SysLog {@b@@b@    /**@b@     * 操作编码@b@     * @return@b@     */@b@    String operateCode();@b@@b@    /**@b@     * 操作类型@b@     * @return@b@     */@b@    String operateType();@b@@b@    /**@b@     * 操作模块描述@b@     * @return@b@     */@b@    String operateModule() default  "";@b@@b@    /**@b@     * 操作描述@b@     * @return@b@     */@b@    String operateDesc()  default "";@b@@b@    /**@b@     *  扩展描述@b@     * @return@b@     */@b@    String extInfo() default "";@b@@b@}

对应报错aop的方法代码

...@b@@SuppressWarnings("rawtypes")@b@public Map<String, Object> getMethodDescription(JoinPoint joinPoint) throws Exception {@b@    Map<String, Object> map = new HashMap<String, Object>();@b@    String targetName = joinPoint.getTarget().getClass().getName();@b@    String methodName = joinPoint.getSignature().getName();@b@    Object[] arguments = joinPoint.getArgs();@b@    Class<?> targetClass = Class.forName(targetName);@b@    Method[] methods = targetClass.getMethods();@b@    for (Method method : methods) {@b@        if (method.getName().equals(methodName)) {@b@            Class[] clazzs = method.getParameterTypes();@b@            if (clazzs.length == arguments.length) {@b@                map.put("operateCode", method.getAnnotation(SysLog.class).operateCode());@b@                map.put("operateType", method.getAnnotation(SysLog.class).operateType());@b@                map.put("args", this.getArgs(method, arguments));@b@                break;@b@            }@b@        }@b@    }@b@    return map;@b@}@b@...

二、解决方法

用如下getMethodDescription方法进行替换,即可解决问题

   ...@b@    @SuppressWarnings("rawtypes")@b@    public Map<String, Object> getMethodDescription(JoinPoint joinPoint) throws Exception {@b@        // 方法签名@b@        Signature signature =  joinPoint.getSignature();@b@        // 获取的是代理类的method对象@b@        Method method = ( (MethodSignature)signature ).getMethod();@b@        // 这个方法才是目标对象上有注解的方法@b@        Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), method.getParameterTypes());@b@        // 取出对应的注解@b@        SysLog authorizationNeed = realMethod.getAnnotation(SysLog.class);@b@@b@        String targetName = joinPoint.getTarget().getClass().getName();@b@        String methodName = joinPoint.getSignature().getName();@b@        Object[] arguments = joinPoint.getArgs();@b@        Class<?> targetClass = Class.forName(targetName);@b@@b@        Map<String, Object> map = new HashMap<String, Object>();@b@        map.put("operateCode", method.getAnnotation(SysLog.class).operateCode());@b@        map.put("operateType", method.getAnnotation(SysLog.class).operateType());@b@        map.put("args", this.getArgs(method, arguments));@b@@b@        return map;@b@    }@b@    ...
<<推荐下载>>
  • (1) 阿里巴巴Java开发手册8种不同版本
  • (2) Web前端开发视频教程
  • (3) 30+明星讲师PPT课件分享一线大厂架构实战经验
  • (4) java开发_架构篇_视频资源分享_v2208
  • (5) java开发_高级篇_视频资源分享_v2208
  • (6) java开发_进阶篇(中级)_视频资源分享_v2208
  • (7) java开发_入门篇_视频资源分享_v2208
  • (8) 微信小程序开发视频1+167源码+实战demo等下载
  • (9) easy-shopping电子商务java源码(附脚本和安装文档说明)下载
  • (10) java常用的72份知名实用的电子书下载
  • (11) java开发性能优化资料整理大全(8份电子文档+3份实战优化)下载
  • (12) 9个常用的算法设计资料和100以上视频课件内容下载
  • (13) vue开发必备常用手册16件下载
  • (14) 21种不同技术集群方案(es、flink、redis、nginx、zk、lvs、kafka、mysql、k8s等)参考资料下载
  • (15) 20种技术代码规范(js/java/dba/阿里/华为/oracle/mysql等)参考资料下载
  • (16) 微服务五套资料(0-1,架构设计,springcloud,nacos等)下载
  • (17) 架构师(28知识图谱+3套简历模板+6套架构实战文档等)完整资料整理下载
  • (18) 大数据18套实战基础知识+8套简历模板下载
  • (19) 并发编程全套(7套+阿里巴巴+亿级实战等)实战资料下载
  • (20) Kafka九套学习整理知识点全套(面试+笔记+代码api+命令+容备等)资料下载
  • (21) java全套9个不同方向类型的面试题(基础+核心+大厂+架构师+近万套题库等)下载
  • (22) JAVA开发常用API帮助文档大全(超52种以上技术资料,高手必备)下载
  • (23) springcloud超详细139件全套学习实战资料( 视频课件+源码demo+文档资料等)下载
  • 更多推荐>>
  • <<热门文章>>