BeanUtils.copyProperties(source, target)报NullPointException

遇到一件怪事,今天在测试一段代码时老时报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包。到此,真相大白了。



 

posted @   zjhgx  阅读(469)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示