llizlcheny 2022-07-10 00:00 采纳率: 66.7%
浏览 209
已结题

测试查询出现空指针异常,如何解决?

查询功能的实现
测试时,获取pageInfo出现空指针异常

1、index.jsp 页面:

<%--
  Created by IntelliJ IDEA.
  User: liulizheng
  Date: 2022/7/9
  Time: 17:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>员工列表</title>
</head>
<body>

</body>
</html>

2、list.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: liulizheng
  Date: 2022/7/9
  Time: 17:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>员工列表</title>
</head>
<body>

</body>
</html>

3、EmployeeService类

package com.liiulizheng.crud.service;

import com.liiulizheng.crud.bean.Employee;
import com.liiulizheng.crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
@author liulizheng
@date 2022-07-09 17:11
*/
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    /**
     * 查询所有员工
     * @return
     */
    public List<Employee> getAll() {

        return employeeMapper.selectByExampleWithDept(null);

    }
}

4、引入PageHelper分页插件
①依赖

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
</dependency>

②注册

<plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

5、EmployeeController类

package com.liiulizheng.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liiulizheng.crud.bean.Employee;
import com.liiulizheng.crud.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 *
 * 处理员工CRUD请求
 * @author liulizheng
 * @date 2022-07-09 17:00
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;
    /**
     * 查询员工数据(分页查询)
     * @return
     */
    @RequestMapping("/emps")
    public String getEmps(@RequestParam(value = "pn", defaultValue = "1")Integer pn, Model model){
        //这不是一个分页查询
        //引入PageHelper分页插件
        //在查询之前只需要调用,传入页码,以及每页的大小
        PageHelper.startPage(pn,5);
        //startPage后面紧跟的这个查询就是一个分页查询
        List<Employee> emps = employeeService.getAll();
        //用PageInfo对结果进行包装,只需要pageInfo交给页面就行了
        //封装了详细的分页信息,包括有我们查询出来的数据,传入连续显示的页数
        PageInfo page = new PageInfo(emps,5);
        model.addAttribute("pageInfo",page);

        return "list";
    }

}

6、虚拟Mvc测试


package com.liiulizheng.crud.test;

import com.github.pagehelper.PageInfo;
import com.liiulizheng.crud.bean.Employee;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.text.Normalizer;
import java.util.List;

/**
 * 使用Spring测试模块提供的测试请求,测试curd请求的正确性
 * @author liulizheng
 * @date 2022-07-09 21:22
 */

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml","file:E:\\MyProword\\IDEA\\ssm-crud\\src\\main\\webapp\\WEB-INF\\dispatcherServlet-servlet.xml"})
public class MvcTest {

    //传入Springmvc的ioc
    @Autowired
    WebApplicationContext context;
    //虚拟mvc请求,获取到处理结果
    MockMvc mockMvc;

    @Before
    public void initMockMvc(){
        //初始化mockMvc
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void testPage() throws Exception {
        //模拟请求,拿到返回值
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).andReturn();

        //请求成功后 请求域中会有pageInfo,可以取出pageInfo进行验证
        MockHttpServletRequest request = result.getRequest();
        PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
        System.out.println("当前页码:" + pi.getPageNum());
        System.out.println("总页码:" + pi.getPages());
        System.out.println("总记录数:" + pi.getTotal());
        System.out.println("在页面需要连续显示的页码");
        int[] nums = pi.getNavigatepageNums();
        for(int i : nums){
            System.out.println(" " + i);
        }

        //获取员工数据
        List<Employee> list = pi.getList();
        for (Employee employee : list){
            System.out.println("ID: " + employee.getEmpId() + "===>Name:" + employee.getEmpName());
        }

    }


}

7、空指针异常

D:\Environment\Java\jdk1.8.0_202\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\development tool\Idea\IntelliJ IDEA 2021.2.2\lib\idea_rt.jar=59295:D:\development tool\Idea\IntelliJ IDEA 2021.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\development tool\Idea\IntelliJ IDEA 2021.2.2\lib\idea_rt.jar;D:\development tool\Idea\IntelliJ IDEA 2021.2.2\plugins\junit\lib\junit5-rt.jar;D:\development tool\Idea\IntelliJ IDEA 2021.2.2\plugins\junit\lib\junit-rt.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\charsets.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\deploy.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\javaws.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\jce.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\jfr.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\jsse.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\management-agent.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\plugin.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\resources.jar;D:\Environment\Java\jdk1.8.0_202\jre\lib\rt.jar;E:\MyProword\IDEA\ssm-crud\target\classes;D:\Environment\apache-maven-3.8.3\maven-repo\org\mybatis\generator\mybatis-generator-core\1.3.7\mybatis-generator-core-1.3.7.jar;D:\Environment\apache-maven-3.8.3\maven-repo\com\github\pagehelper\pagehelper\5.0.0\pagehelper-5.0.0.jar;D:\Environment\apache-maven-3.8.3\maven-repo\com\github\jsqlparser\jsqlparser\0.9.5\jsqlparser-0.9.5.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-webmvc\5.3.20\spring-webmvc-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-aop\5.3.20\spring-aop-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-beans\5.3.20\spring-beans-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-context\5.3.20\spring-context-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-core\5.3.20\spring-core-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-jcl\5.3.20\spring-jcl-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-expression\5.3.20\spring-expression-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-web\5.3.20\spring-web-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-jdbc\5.3.20\spring-jdbc-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-tx\5.3.20\spring-tx-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-aspects\5.3.20\spring-aspects-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\aspectj\aspectjweaver\1.9.7\aspectjweaver-1.9.7.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;D:\Environment\apache-maven-3.8.3\maven-repo\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;D:\Environment\apache-maven-3.8.3\maven-repo\mysql\mysql-connector-java\8.0.28\mysql-connector-java-8.0.28.jar;D:\Environment\apache-maven-3.8.3\maven-repo\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;D:\Environment\apache-maven-3.8.3\maven-repo\jstl\jstl\1.2\jstl-1.2.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\springframework\spring-test\5.3.20\spring-test-5.3.20.jar;D:\Environment\apache-maven-3.8.3\maven-repo\javax\servlet\javax.servlet-api\4.0.1\javax.servlet-api-4.0.1.jar;D:\Environment\apache-maven-3.8.3\maven-repo\junit\junit\4.13.1\junit-4.13.1.jar;D:\Environment\apache-maven-3.8.3\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.liiulizheng.crud.test.MvcTest,testPage
七月 09, 2022 11:54:23 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
七月 09, 2022 11:54:23 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@2ac273d3, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@71423665, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@20398b7c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6fc6f14e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@56235b8e, org.springframework.test.context.transaction.TransactionalTestExecutionListener@3632be31, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5abca1e0, org.springframework.test.context.event.EventPublishingTestExecutionListener@2286778]
七月 09, 2022 11:54:24 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
七月 09, 2022 11:54:24 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
七月 09, 2022 11:54:25 下午 org.springframework.mock.web.MockServletContext log
信息: Initializing Spring TestDispatcherServlet ''
七月 09, 2022 11:54:25 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: Initializing Servlet ''
七月 09, 2022 11:54:25 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: Completed initialization in 54 ms

java.lang.NullPointerException
    at com.liiulizheng.crud.test.MvcTest.testPage(MvcTest.java:57)   // 这个语句PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
    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:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    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:366)
    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$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    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$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)


Process finished with exit code -1
  • 写回答

2条回答 默认 最新

  • 一头小山猪 一头小山猪(个人IP)官方账号 2022-07-10 10:55
    关注

    空指针表示request.getAttribute("pageInfo");得到的内容为空。
    假设你的请求调用正确,那需要检查一下是否查到了数据,可以在
    employeeService.getAll();
    附近打断点看一下是否有数据,因为你没有打开MyBatis的日志,所以看不到数据的查询情况,可以自己先检查一下。
    有帮助请采纳,还有不懂的可以继续追问~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月20日
  • 已采纳回答 7月12日
  • 创建了问题 7月10日

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀