博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
不学无数——Spring注入后反射的空指针异常
阅读量:6411 次
发布时间:2019-06-23

本文共 6461 字,大约阅读时间需要 21 分钟。

Spring注入后反射的空指针异常

1. 异常说明

在单元测试类中,要单独测一个类中的private方法,所以想通过反射获得此方法,解除私有限定,然后进行调用。此类为Service其中注入了许多的Mapper,因此在通过调用此私有方法的时候报了空指针异常的错误,错误如下:

java.lang.reflect.InvocationTargetException	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	at java.lang.reflect.Method.invoke(Method.java:498)	at com.suixingpay.bap.service.impl.CheckAmsPmtAndPtsFlowServiceImplTest.testInvoke(CheckAmsPmtAndPtsFlowServiceImplTest.java:78)	at com.suixingpay.bap.service.impl.CheckAmsPmtAndPtsFlowServiceImplTest.insertByList(CheckAmsPmtAndPtsFlowServiceImplTest.java:69)	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	at java.lang.reflect.Method.invoke(Method.java:498)	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)Caused by: java.lang.NullPointerException	at com.suixingpay.bap.service.impl.CheckAmsPmtAndPtsFlowServiceImpl.succesExitReove(CheckAmsPmtAndPtsFlowServiceImpl.java:247)	... 36 more复制代码

注意最后面是由NullPointerException引起的。通过Debug进去以后发现是与数据库交互的Mapper没有注入进来。

单元测试代码如下:

@Autowired    private CheckAmsPmtAndPtsFlowServiceImpl checkAmsPmtAndPtsFlowServiceI;        @Test    public void insertByList()            throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException,            NoSuchFieldException {        List
list = new ArrayList<>(); Class cls = Class.forName("service.impl.CheckAmsPmtAndPtsFlowServiceImpl"); Class[] cArg = new Class[5]; cArg[0] = List.class; cArg[1] = boolean.class; cArg[2] = String.class; cArg[3] = String.class; cArg[4] = String.class; Method succesExitReove = cls.getDeclaredMethod("succesExitReove",cArg); succesExitReove.setAccessible(true); succesExitReove.invoke(checkAmsPmtAndPtsFlowServiceI,list,true,"1","1","2"); }复制代码

此处有个知识点:在调用getDeclaredMethod()获得指定的方法时,第二个参数是方法的传参类型,包装类和其基本类型是不一样,例如下面方法为boolean,如果此时在上面 cArg[1] = Boolean.class;写为其包装类型的话,那么就会报错找不到此方法的错误

被反射的类中方法succesExitReove ()如下

@Autowired	private TMemAcPmtDtlPoMapper tMemAcPmtDtlPoMapper;		private void succesExitReove(List
result, boolean isFen, String sourceType, String opId, String batchId) { Iterator
iteDownPtsNoCodeSuccces = result.iterator(); while (iteDownPtsNoCodeSuccces.hasNext()) { int count; if (isFen) { count = tMemAcPmtDtlPoMapper.queryCheckFlow(bapCheckResult.getOutTransId()); } else { count = tMemAcPmtDtlPoMapper.queryCheckFlow(bapCheckResult.getTransId()); } bapCheckResult.setPmtCount(count); if (count > 0) { iteDownPtsNoCodeSuccces.remove(); } } }复制代码

此处在Debug时发现tMemAcPmtDtlPoMapper为空。

2. 解决办法

在单元测试类中@Autowired要用的Mapper,然后通过反射获得上面方法要用到Mapper的变量,为其赋值即可

单元测试代码如下

@Autowired    private CheckAmsPmtAndPtsFlowServiceImpl checkAmsPmtAndPtsFlowServiceI;    --在单元测试类中进行依赖注入Mapper    @Autowired    private TMemAcPmtDtlPoMapper tMemAcPmtDtlPoMapper;        @Test    public void insertByList()            throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException,            NoSuchFieldException {        List
list = new ArrayList<>(); Class cls = Class.forName("service.impl.CheckAmsPmtAndPtsFlowServiceImpl"); Class[] cArg = new Class[5]; cArg[0] = List.class; cArg[1] = boolean.class; cArg[2] = String.class; cArg[3] = String.class; cArg[4] = String.class; Field a1 = cls.getDeclaredField("tMemAcPmtDtlPoMapper"); Method succesExitReove = cls.getDeclaredMethod("succesExitReove",cArg); succesExitReove.setAccessible(true); --在此为反射的类进行赋值Mapper Field a1 = cls.getDeclaredField("tMemAcPmtDtlPoMapper"); a1.setAccessible(true); a1.set(checkAmsPmtAndPtsFlowServiceI,tMemAcPmtDtlPoMapper); succesExitReove.invoke(checkAmsPmtAndPtsFlowServiceI,list,true,"1","1","2"); }复制代码

转载地址:http://okkra.baihongyu.com/

你可能感兴趣的文章
Mac上搭建ELK
查看>>
443 Chapter7.Planning for High Availability in the Enterprise
查看>>
HttpHandler初探 - 页面上输出图像
查看>>
框架和语言的作用
查看>>
unidac连接ORACLE免装客户端驱动
查看>>
Cygwin + OpenSSH FOR Windows的安装配置
查看>>
咏南中间件支持手机客户端
查看>>
fastscript增加三方控件之二
查看>>
Windows Vista RTM 你准备好了么?
查看>>
Tensorflow Serving 模型部署和服务
查看>>
Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet 3.0+JSP 2.2深入剖析与实例应用
查看>>
topcoder srm 680 div1 -3
查看>>
具体数学第二版第四章习题(1)
查看>>
高效前端优化工具--Fiddler入门教程
查看>>
【翻译】我钟爱的HTML5和CSS3在线工具
查看>>
Java多线程学习(吐血超详细总结)
查看>>
css3 变形
查看>>
Win7 64bit 安装Mysql5 出错 无法启动服务。
查看>>
嵌入式 H264参数语法文档: SPS、PPS、IDR以及NALU编码规律
查看>>
初识Opserver,StackExchange的监控解决方案
查看>>