报错
nested exception is java.lang.NullPointerException 空指针异常
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-18 16:44:55.380 ERROR 2056 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl' defined in file [W:\~IDEA\web_class\back_end\target\classes\com\example\web_class\service\impl\UserServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.web_class.service.impl.UserServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
Error building SqlSession.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.web_class.service.impl.UserServiceImpl]: Constructor threw exception; nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in mapper/UserMapper.xml
错误原因及解决方案
过程分析(排错的过程、思路)
项目结构:
错误原因:忘记加注解,或者注解加错了
- UserController调用userMapper接口
package com.example.web_class.controller;
......
@Autowired /****** 1. @Autowired 忘记加会出现报错!******/
private UserService userService;
@PostMapping("login")
public Result login(
@RequestParam(value = "username",required = false) String username,
@RequestParam(value = "password",required = false) String password,
ModelAndView modelAndView){
User user = User.builder().username(username).password(password).build();
//userService被调用
String data = userService.login(user);
return data!=null? ResultGenerator.genSuccessResult(data):ResultGenerator.genFailResult("账号密码错误!");
};
- 查看userService接口
package com.example.web_class.service;
......
@Service /****** 2. @Service 忘记加会出现报错!******/
public interface UserService {
String login(User user);
}
- userService接口的实现 implement
package com.example.web_class.service.impl;
......
@Service /****** 3. @Service 忘记加会出现报错!******/
public class UserServiceImpl implements UserService{
String resource= "config/mybatis-config.xml";
// JAVA中getResourceAsStream这个方法是用来获取配置文件
InputStream inputStream= Resources.class.getResourceAsStream((resource));
/******** 接下来这几句就是报出空指针异常的语句!!! **********/
InputStream inputStream= Resources.class.getResourceAsStream((resource));
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession(true);
UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
......
}
配置文件的路径写错了
String resource= "config/mybatis-config.xml";
IDEA 无法识别java包下面的xml文件。可以修改pom文件识别。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
配置文件里面mapper.xml的路径写错了
//mybatis-config.xml
......
<mappers>
<!-- <mapper resource="com/example/web_class/mapper/UserMapper.xml"/>-->
<mapper resource="mapper/UserMapper.xml"/>
<mapper resource="mapper/MailMapper.xml"/>
</mappers>
......
A 无法识别java包下面的xml文件,同样可以修改pom文件识别。xml路径中不能时com.example.
这样的,要用/
,同时注意url、resource等的区别。
超级难以难以察觉到的错误!导入错误的包!(resources)
import org.apache.ibatis.io.Resources;
...
//正确的resources包应该是 org.apache.ibatis.io.Resources;
InputStream inputStream= Resources.getResourceAsStream((resource));