错误:(在我的spring crud操作项目中抛出异常导致根本原因错误)
Error: Nov 14, 2017 12:43:12 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring] in context with path
[/Spring4MVCAngularJSExampleLatest] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.websystique.springmvc.service.UserServiceImpl.saveUser(UserServiceImpl.java:56)
at
com.websystique.springmvc.controller.HelloWorldRestController.createUser(HelloWorldRestController.java:69)
(这笔款项是在订婚结束时一次性支付的。我亲切的主人,应我的要求,给了我一笔一笔款项来代替我的年金。我建议一次性付款,条件是你退出这个诉讼不是一次性拨款,而是通过拨款或资金。)
代码:
JavaService Impl:
public void saveUser(User user)
{
System.out.println(user.getAddress()+""+user.getId());//printing successfully
// user.setId(1);
((UserDaoImpl) users).saveOrUpdate(user);// i got error here
//ud.saveOrUpdate(user);
}
=============================================== ==
UserService.java这是服务类
public interface UserService {
User findById(long id);
User findByName(String name);
void saveUser(User user);
void updateUser(User user);
void deleteUserById(long id);
List<User> findAllUsers();
void deleteAllUsers();
public boolean isUserExist(User user);
}
=============================================== =========
UserDaoImpl.java :(这是存储库impl类)
public List<User> list() {
String sql = "SELECT * FROM user12";
List<User> listContact = jdbcTemplate.query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User aContact = new User();
aContact.setId(rs.getInt("id"));
aContact.setUsername(rs.getString("username"));
aContact.setEmail(rs.getString("email"));
aContact.setAddress(rs.getString("address"));
System.out.println("ASADADADAS" + rs.getString("username"));
return aContact;
}
});
=================================
HelloWorldRestController.java(这是控制器类)
@RequestMapping(value = "/user/", method = RequestMethod.GET)
public ResponseEntity<List<User>> listAllUsers() {
List<User> users = userService.findAllUsers();
/* if(users.isEmpty()){
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
}*/
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
=============================
UserDao.java(这是存储库类)
public interface UserDao {
public void saveOrUpdate(User user);
public void delete(int id);
public User get(long id);
public List<User> list();
}
答案 0 :(得分:0)
In the service the ((UserDaoImpl) users)
is null. It means it was not injected properly.
The UserDaoImpl must be annotated with @Repository (or @Component) to be a spring bean. Also it should be in component scan package to let Spring find it and create a bean.