目录
问题:
java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "obj" is null
at java.base/java.lang.reflect.Method.invoke(Method.java:570)
at org.myspringframework.core.ClassPathXmlApplicationContext.lambda$new$0(ClassPathXmlApplicationContext.java:172)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.myspringframework.core.ClassPathXmlApplicationContext.lambda$new$1(ClassPathXmlApplicationContext.java:83)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.myspringframework.core.ClassPathXmlApplicationContext.lambda$new$2(ClassPathXmlApplicationContext.java:69)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.myspringframework.core.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:43)
at com.dong.myspring.Test.Test.main(Test.java:10)
问题指向:
解决:
该问题是在一个空对象上调用方法而导致的,所以增加一个非空判断即可
if (singletonObject.get(ref) != null) { setMethod.invoke(singletonObject.get(id), singletonObject.get(ref)); }
运行正常:
来源地址:https://blog.csdn.net/m0_63064861/article/details/131821151