学习SSM框架分页插件查询时出现空指针异常,求解!!!

异常代码:

java.lang.NullPointerException
    at com.atguigu.crud.test.MvcTest.testPage(MvcTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    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.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:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    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:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

测试了一下:

    MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();
        MockHttpServletRequest request = result.getRequest();

        System.out.println("1:"+request);

        System.out.println("2:"+request.getAttribute("pageInfo"));

结果:
1:org.springframework.mock.web.MockHttpServletRequest@4a067c25
2:null

是在执行了getAttribute()这个方法后出现的异常

代码检查了几遍了,不知道是不是哪个配置文件出了问题。

weixin_43538368
A.I.C.O 怎么解决的?我也遇到这问题了
9 个月之前 回复
qq_36018710
Dfse0 springmvc配置文件有误
一年多之前 回复

3个回答

springmvc配置文件扫描设置有误

com.atguigu.crud.test.MvcTest.testPage(MvcTest.java:53) 控制台都明确打印了这一行报的错; 检查一下这一行的对象

weixin_44448463
Hard-Yang 解决了吗,怎么解决的
4 个月之前 回复
sinat_34443859
ITxiaocaifei 回复Dfse0: 解决了吗?
一年多之前 回复
qq_36018710
Dfse0 在request.getAttribute("pageInfo")这里就null了 被赋值的对象也是空 然后后面就报错了
一年多之前 回复
qq_36018710
Dfse0 PageInfo pi = (PageInfo) request.getAttribute("pageInfo"); System.out.println("当前页码:"+pi.getPageNum());
一年多之前 回复

我的也是碰见了同样的问题
之前是碰见加载配置文件applicationContext.xml失败,经修正发现是service类上没有标明注解@service
解决上面那个问题之后就有空指针异常,请求域中不能接收数据,打印pi为空值
然后发现是因为controller类中的遍历所有员工信息这一个类上少了一个@RequestMapping("/emps")注解用以接收请求
至此问题解决,希望对你有帮助

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐