请在多线程场景中提供帮助,但我很少在下面的代码中获得NullPointerException
public class CassPerfInterceptor {
private static final Logger log = LoggerFactory
.getLogger(CassPerfInterceptor.class);
private Performance performance;
@Autowired
private QueueServerMbean queueServerMbean;
@Around("target(org.springframework.data.repository.CrudRepository) || execution(public * com.owmessaging.*.dao.*.*Dao.*(..))")
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
try {
Object obj = joinPoint.proceed();
performance = StatsProvider.getCassandraStats(joinPoint.getSignature().getName());
if(performance != null){
// Here I am getting NPE line number 30
performance.add(System.currentTimeMillis()-start, false);
}
return obj;
}catch(DriverException de){
queueServerMbean.sendNotification("CRITICAL", "DB Connectivity Error: "+de.getMessage());
throw de;
}
catch(Exception e){
log.error("Error: ",e);
performance = StatsProvider.getCassandraStats(joinPoint.getSignature().getName());
if(performance != null){
performance.add(System.currentTimeMillis()-start, true);
}
throw e;
}
}
}
下面是堆栈跟踪:
11126 2016-07-04 17:16:58,448[http-nio-8014-exec-16]ERROR CassPerfInterceptor -Error:
11127
java.lang.NullPointerException
11128 at com.owmessaging.qserv.stats.CassPerfInterceptor.invoke(CassPerfInterceptor.java:31)
11129 at sun.reflect.GeneratedMethodAccessor39.invoke(Unknown Source)
11130 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11131 at java.lang.reflect.Method.invoke(Method.java:497)
11132 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
11133 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
11134 at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
11135 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)
11136 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
11137 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
11138 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
11139 at com.sun.proxy.$Proxy79.insertMsgInfo(Unknown Source)
11140 at com.owmessaging.qserv.service.impl.QueueServiceImpl.enqueue(QueueServiceImpl.java:151)
即使我应用预防性检查if (performance != null)
,我仍然会获得NPE。上面检查没有线程安全。 (但请注意,这是非常罕见的情况,我得到了7000个请求中的NPE。)