I am getting the error from my code running LoginController even after adding @Autowired to "private IUserService userService;". Could someone please help? Thanks a lot!
"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null"
Error Msg enter image description here
Code
@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {
@Autowired
// no autowired, nullpointerexception
// autowired, cannot receive response
private IUserService userService;
@RequestMapping("/toLogin") //to log in page 跳转登录页
public String toLogin(){
return "login";
}
@RequestMapping("/doLogin")
@ResponseBody
public RespBean doLogin(LoginVo loginVo) {
// 接受传参 receive parameter from users with LoginVo
return userService.doLogin(loginVo);
}
}
public interface IUserService extends IService<User> {
RespBean doLogin(LoginVo loginVo);
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
private UserMapper userMapper;
@Override
public RespBean doLogin(LoginVo loginVo) {
String mobile = loginVo.getMobile();
String password = loginVo.getPassword();
// check whether input is empty
if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
return RespBean.error(RespBeanEnum.LOGIN_ERROR);
}
// check whether mobile is valid
if (!ValidatorUtil.isMobile(mobile)) {
return RespBean.error(RespBeanEnum.MOBILE_ERROR);
}
// find user with mobile
User user = userMapper.selectById(mobile);
if (user == null) {
return RespBean.error(RespBeanEnum.LOGIN_ERROR);
}
// verify whether user password is correct
if (!MD5Util.frontPassToDBPass(password, user.getSalt()).equals(user.getPassword())) {
return RespBean.error(RespBeanEnum.LOGIN_ERROR);
}
return RespBean.success();
}
}
Without @Autowired, the login page responses to user input with NullPointerException above anyway. With @Autowired added, if mobile input is invalid, there is no response. If mobile and password input is valid, the login page responses with NullPointerException above.
I tried adding qualifier with @Component according to website below but it doesn't work.