李晓彬Sir
2022-02-14 14:13
采纳率: 100%
浏览 177

Mybatis-Plus 空指针异常

public class User {
    @TableId(
            value = "id",
            type = IdType.AUTO
    )
    private Integer id;
    private String name;
    private Integer age;
    private String email;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                '}';
    }
}

mapper

package lixiaobin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lixiaobin.entity.User;
/*
* 1.Mapper,就是Dao接口
* 2.指定实体类
*
*BaseMapper是MP框架中的对象,定义了17个操作方法
*
* */
public interface UserMapper extends BaseMapper<User> {

}


Application

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
* @MapperScan 扫描mapper文件
* */
@SpringBootApplication
@MapperScan("lixiaobin.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


测试类

import lixiaobin.entity.User;
import lixiaobin.mapper.UserMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;


@SpringBootTest

public class Application {
    //使用自动注入
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testSelect(){
        System.out.println("-------SelectAll method test ---------");
        List<User> users = userMapper.selectList(null);
        Assert.assertEquals(5,users.size());
        users.forEach(System.out::println);
    }
}

空指针异常

C:\Users\李晓彬.jdks\corretto-1.8.0_302\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.4\lib\idea_rt.jar=53990:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.4\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.4\plugins\junit\lib\junit5-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.4\plugins\junit\lib\junit-rt.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\charsets.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\access-bridge-64.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\cldrdata.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\dnsns.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\jaccess.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\jfxrt.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\localedata.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\nashorn.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\sunec.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\sunjce_provider.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\sunmscapi.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\sunpkcs11.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\ext\zipfs.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\jce.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\jfr.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\jfxswt.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\jsse.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\management-agent.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\resources.jar;C:\Users\李晓彬.jdks\corretto-1.8.0_302\jre\lib\rt.jar;C:\Users\李晓彬\IdeaProjects\Mybatis-Plus\target\test-classes;C:\Users\李晓彬\IdeaProjects\Mybatis-Plus\target\classes;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot-starter\2.6.3\spring-boot-starter-2.6.3.jar;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot\2.6.3\spring-boot-2.6.3.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-context\5.3.15\spring-context-5.3.15.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-aop\5.3.15\spring-aop-5.3.15.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-beans\5.3.15\spring-beans-5.3.15.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-expression\5.3.15\spring-expression-5.3.15.jar;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.6.3\spring-boot-autoconfigure-2.6.3.jar;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.6.3\spring-boot-starter-logging-2.6.3.jar;C:\Users\李晓彬.m2\repository\ch\qos\logback\logback-classic\1.2.10\logback-classic-1.2.10.jar;C:\Users\李晓彬.m2\repository\ch\qos\logback\logback-core\1.2.10\logback-core-1.2.10.jar;C:\Users\李晓彬.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.1\log4j-to-slf4j-2.17.1.jar;C:\Users\李晓彬.m2\repository\org\apache\logging\log4j\log4j-api\2.17.1\log4j-api-2.17.1.jar;C:\Users\李晓彬.m2\repository\org\slf4j\jul-to-slf4j\1.7.33\jul-to-slf4j-1.7.33.jar;C:\Users\李晓彬.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-core\5.3.15\spring-core-5.3.15.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-jcl\5.3.15\spring-jcl-5.3.15.jar;C:\Users\李晓彬.m2\repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot-starter-test\2.5.6\spring-boot-starter-test-2.5.6.jar;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.5.6\spring-boot-test-autoconfigure-2.5.6.jar;C:\Users\李晓彬.m2\repository\com\jayway\jsonpath\json-path\2.5.0\json-path-2.5.0.jar;C:\Users\李晓彬.m2\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;C:\Users\李晓彬.m2\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;C:\Users\李晓彬.m2\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;C:\Users\李晓彬.m2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\Users\李晓彬.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;C:\Users\李晓彬.m2\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;C:\Users\李晓彬.m2\repository\org\assertj\assertj-core\3.19.0\assertj-core-3.19.0.jar;C:\Users\李晓彬.m2\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;C:\Users\李晓彬.m2\repository\org\junit\jupiter\junit-jupiter\5.7.2\junit-jupiter-5.7.2.jar;C:\Users\李晓彬.m2\repository\org\junit\jupiter\junit-jupiter-api\5.7.2\junit-jupiter-api-5.7.2.jar;C:\Users\李晓彬.m2\repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;C:\Users\李晓彬.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\李晓彬.m2\repository\org\junit\platform\junit-platform-commons\1.7.2\junit-platform-commons-1.7.2.jar;C:\Users\李晓彬.m2\repository\org\junit\jupiter\junit-jupiter-params\5.7.2\junit-jupiter-params-5.7.2.jar;C:\Users\李晓彬.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.7.2\junit-jupiter-engine-5.7.2.jar;C:\Users\李晓彬.m2\repository\org\junit\platform\junit-platform-engine\1.7.2\junit-platform-engine-1.7.2.jar;C:\Users\李晓彬.m2\repository\org\mockito\mockito-core\3.9.0\mockito-core-3.9.0.jar;C:\Users\李晓彬.m2\repository\net\bytebuddy\byte-buddy\1.10.20\byte-buddy-1.10.20.jar;C:\Users\李晓彬.m2\repository\net\bytebuddy\byte-buddy-agent\1.10.20\byte-buddy-agent-1.10.20.jar;C:\Users\李晓彬.m2\repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;C:\Users\李晓彬.m2\repository\org\mockito\mockito-junit-jupiter\3.9.0\mockito-junit-jupiter-3.9.0.jar;C:\Users\李晓彬.m2\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;C:\Users\李晓彬.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-test\5.3.12\spring-test-5.3.12.jar;C:\Users\李晓彬.m2\repository\org\xmlunit\xmlunit-core\2.8.3\xmlunit-core-2.8.3.jar;C:\Users\李晓彬.m2\repository\com\baomidou\mybatis-plus-boot-starter\3.5.1\mybatis-plus-boot-starter-3.5.1.jar;C:\Users\李晓彬.m2\repository\com\baomidou\mybatis-plus\3.5.1\mybatis-plus-3.5.1.jar;C:\Users\李晓彬.m2\repository\com\baomidou\mybatis-plus-extension\3.5.1\mybatis-plus-extension-3.5.1.jar;C:\Users\李晓彬.m2\repository\com\baomidou\mybatis-plus-core\3.5.1\mybatis-plus-core-3.5.1.jar;C:\Users\李晓彬.m2\repository\com\baomidou\mybatis-plus-annotation\3.5.1\mybatis-plus-annotation-3.5.1.jar;C:\Users\李晓彬.m2\repository\com\github\jsqlparser\jsqlparser\4.3\jsqlparser-4.3.jar;C:\Users\李晓彬.m2\repository\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;C:\Users\李晓彬.m2\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.5.3\spring-boot-starter-jdbc-2.5.3.jar;C:\Users\李晓彬.m2\repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-jdbc\5.3.9\spring-jdbc-5.3.9.jar;C:\Users\李晓彬.m2\repository\org\springframework\spring-tx\5.3.9\spring-tx-5.3.9.jar;C:\Users\李晓彬.m2\repository\org\springframework\boot\spring-boot-test\2.2.6.RELEASE\spring-boot-test-2.2.6.RELEASE.jar;C:\Users\李晓彬.m2\repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar;C:\Users\李晓彬.m2\repository\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;C:\Users\李晓彬.m2\repository\junit\junit\4.13.2\junit-4.13.2.jar;C:\Users\李晓彬.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 Application
-------SelectAll method test ---------

java.lang.NullPointerException
at Application.testSelect(Application.java:23)
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.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
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.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
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:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

Process finished with exit code -1

4条回答 默认 最新

相关推荐 更多相似问题