@Controller
@RequestMapping("/userEntity")
public class UserController {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
System.out.println(userService.selectMapper().size());
}
}
问题:
在controller中使用main函数测试 控制台报以下错误:new的实例为null
"C:\Program Files\Java\jdk-11\bin\java.exe" "-javaagent:C:\Program Files (x86)\Tool\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=62241:C:\Program Files (x86)\Tool\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\HongG\Desktop\CODE\test\target\classes;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot-starter-web\2.7.13\spring-boot-starter-web-2.7.13.jar;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot-starter\2.7.13\spring-boot-starter-2.7.13.jar;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot\2.7.13\spring-boot-2.7.13.jar;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot-starter-logging\2.7.13\spring-boot-starter-logging-2.7.13.jar;D:\HG_work\Maven\repository\ch\qos\logback\logback-classic\1.2.12\logback-classic-1.2.12.jar;D:\HG_work\Maven\repository\ch\qos\logback\logback-core\1.2.12\logback-core-1.2.12.jar;D:\HG_work\Maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\HG_work\Maven\repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\HG_work\Maven\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\HG_work\Maven\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\HG_work\Maven\repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot-starter-json\2.7.13\spring-boot-starter-json-2.7.13.jar;D:\HG_work\Maven\repository\com\fasterxml\jackson\core\jackson-databind\2.13.5\jackson-databind-2.13.5.jar;D:\HG_work\Maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.5\jackson-annotations-2.13.5.jar;D:\HG_work\Maven\repository\com\fasterxml\jackson\core\jackson-core\2.13.5\jackson-core-2.13.5.jar;D:\HG_work\Maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.5\jackson-datatype-jdk8-2.13.5.jar;D:\HG_work\Maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.5\jackson-datatype-jsr310-2.13.5.jar;D:\HG_work\Maven\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.5\jackson-module-parameter-names-2.13.5.jar;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.13\spring-boot-starter-tomcat-2.7.13.jar;D:\HG_work\Maven\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.76\tomcat-embed-core-9.0.76.jar;D:\HG_work\Maven\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.76\tomcat-embed-el-9.0.76.jar;D:\HG_work\Maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.76\tomcat-embed-websocket-9.0.76.jar;D:\HG_work\Maven\repository\org\springframework\spring-web\5.3.28\spring-web-5.3.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-beans\5.3.28\spring-beans-5.3.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-webmvc\5.3.28\spring-webmvc-5.3.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-aop\5.3.28\spring-aop-5.3.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-context\5.3.28\spring-context-5.3.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-expression\5.3.28\spring-expression-5.3.28.jar;D:\HG_work\Maven\repository\com\baomidou\mybatis-plus-boot-starter\3.3.1.tmp\mybatis-plus-boot-starter-3.3.1.tmp.jar;D:\HG_work\Maven\repository\com\baomidou\mybatis-plus\3.3.1.tmp\mybatis-plus-3.3.1.tmp.jar;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.7.13\spring-boot-autoconfigure-2.7.13.jar;D:\HG_work\Maven\repository\org\springframework\boot\spring-boot-starter-jdbc\2.7.13\spring-boot-starter-jdbc-2.7.13.jar;D:\HG_work\Maven\repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;D:\HG_work\Maven\repository\org\springframework\spring-jdbc\5.3.28\spring-jdbc-5.3.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-tx\5.3.28\spring-tx-5.3.28.jar;D:\HG_work\Maven\repository\com\baomidou\mybatis-plus-generator\3.3.1\mybatis-plus-generator-3.3.1.jar;D:\HG_work\Maven\repository\com\baomidou\mybatis-plus-extension\3.3.1\mybatis-plus-extension-3.3.1.jar;D:\HG_work\Maven\repository\com\baomidou\mybatis-plus-core\3.3.1\mybatis-plus-core-3.3.1.jar;D:\HG_work\Maven\repository\com\baomidou\mybatis-plus-annotation\3.3.1\mybatis-plus-annotation-3.3.1.jar;D:\HG_work\Maven\repository\com\github\jsqlparser\jsqlparser\3.1\jsqlparser-3.1.jar;D:\HG_work\Maven\repository\org\mybatis\mybatis\3.5.3\mybatis-3.5.3.jar;D:\HG_work\Maven\repository\org\mybatis\mybatis-spring\2.0.3\mybatis-spring-2.0.3.jar;D:\HG_work\Maven\repository\org\apache\velocity\velocity-engine-core\2.3\velocity-engine-core-2.3.jar;D:\HG_work\Maven\repository\org\apache\commons\commons-lang3\3.12.0\commons-lang3-3.12.0.jar;D:\HG_work\Maven\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\HG_work\Maven\repository\com\mysql\mysql-connector-j\8.0.33\mysql-connector-j-8.0.33.jar;D:\HG_work\Maven\repository\org\projectlombok\lombok\1.18.28\lombok-1.18.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-core\5.3.28\spring-core-5.3.28.jar;D:\HG_work\Maven\repository\org\springframework\spring-jcl\5.3.28\spring-jcl-5.3.28.jar com.example.demo.controller.UserController
14:26:28.294 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
Exception in thread "main" java.lang.NullPointerException
at com.example.demo.service.impl.UserServiceImpl.selectMapper(UserServiceImpl.java:28)
at com.example.demo.controller.UserController.main(UserController.java:24)
Process finished with exit code 1
解决办法:
通过测试,如果实例中存在@ Autowired 等注解时,会报null错误,不应该在这里使用new ;
对于我的问题解决办法,就是使用@SpringBootTest 的测试类 测试时继续使用注入