遇到一件怪事,今天在测试一段代码时老时报NullPointException
代码:
--- ObjectUtil.java JdOrder jdOrder = new JdOrder(); jdOrder.setSkuId(132323324l); jdOrder.setCommissionRate(BigDecimal.valueOf(0.53)); jdOrder.setEstimateCosPrice(BigDecimal.valueOf(0.43)); ShopguideOrder order = new ShopguideOrder(); BeanUtils.copyProperties(jdOrder,order);
Exception in thread "main" java.lang.NullPointerException at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:69) at org.springframework.beans.GenericTypeAwarePropertyDescriptor.<init>(GenericTypeAwarePropertyDescriptor.java:71) at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:353) at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:307) at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:188) at org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:360) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:608) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:543) at com.davidhu.shopguide.api.utils.ObjectUtil.main(ObjectUtil.java:128)
看源代码看了不少时间,只时知道类里有一个PropertyDescriptor propertyName
= class,然后这个属性没有writemethod,然后就报错。但这个方法是copy对象属性的常用方法,不应该有问题呀。然后我把代码拷贝到
ThirdApiTest里又跑了一下,奇怪,在这里又没问题了
google了一下,果然还是找到了
https://github.com/spring-projects/spring-framework/issues/24177
Issue: Failure in BeanUtils.copyProperties(source, target) Stack Trace: Exception in thread "main" java.lang.NullPointerException at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:69) at org.springframework.beans.GenericTypeAwarePropertyDescriptor.(GenericTypeAwarePropertyDescriptor.java:70) at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:359) at org.springframework.beans.CachedIntrospectionResults.(CachedIntrospectionResults.java:316) at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:186) at org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:360) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:608) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:579) Initial Analysis: findBridgedMethod(Method bridgeMethod) method of BridgeMethodResolver was refactored as part of Spring 5.0 release. Whenever this method is invoked for getting writeMethodToUse for propertyName = class, it results in Null Pointer exception as isBridge() method of bridgeMethod is invoked which is null.
回答:
Contributor jhoeller commented on Dec 10, 2019 It seems that you're running spring-beans-4.3.x against spring-core-5.x.x there. We don't support that, the Spring Framework module versions need to be in sync. Please make sure that your classpath is consistent in that respect. spring-beans-5.x.x has corresponding non-null guards at that position so bypasses the BridgeMethodResolver call upfront.
焕然大悟,原来在ObjectUtil.java用到的spring-beans-4.0.9,而运行在ThirdApiTest 里跑的是spring-beans-5.3.1,因为ObjectUtil.java所在工程是api,而ThirdApiTest所在工程是admin。
那为什么api用的是spring-beans-4.0.9?在api下面有个idea生成的文件api.iml,上面有
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.0.9.RELEASE" level="project" />
删掉重新生成,还是依赖了4.0.9包。分析了api的pom.xml,发现这个spring是依赖于swagger的,因为api没有单独引入spring的任何jar包。到此,真相大白了。