今天在写这一段代码后运行时出现了引用的对象是空指针的问题:
@Override
public List<Comment> findComments(Integer id) {
CommentExample commentExample = new CommentExample();
CommentExample.Criteria criteria = commentExample.createCriteria();
criteria.andMoviesIdEqualTo(id);
commentExample.setOrderByClause("time desc");
//出现错误的代码行(空指针):↓↓↓
List<Comment> comments = commentMapper.selectByExample(commentExample);
return comments;
}
@Override
public List<Comment> saveComment(Comment comment) {
commentMapper.insert(comment);
return new MovieMaServiceImpl().findComments(comment.getid());
}
其中第一个方法的
List comments = commentMapper.selectByExample(commentExample);
出现了空指针异常,经过检查,发现是在这个类中我只创建了一个成员对象commentMapper,而在下面的方法正在占用这个成员对象时另一个方法也被调用并需要去使用同一个成员对象时,因为先调用的方法并没有弹栈释放资源,导致无法获取到commentMapper,所以出现了空指针.
2021.1.14.18.05