添加@Autowired后LoginController仍报NullPointerException求助
问题描述
我在LoginController中给private IUserService userService添加@Autowired注解后,运行代码仍抛出空指针异常:
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
- 未加
@Autowired时,登录页处理用户输入始终报上述空指针 - 加了
@Autowired后,输入无效手机号时无响应,输入有效手机号和密码时还是报空指针 - 试过结合
@Component加@Qualifier注解,问题没解决
相关代码
LoginController.java
@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); } }
IUserService.java
public interface IUserService extends IService<User> { RespBean doLogin(LoginVo loginVo); }
UserServiceImpl.java
@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(); } }
问题排查与解决
1. 核心空指针原因:UserServiceImpl中的UserMapper未注入
你的UserServiceImpl里private UserMapper userMapper;没有添加任何注入注解,调用userMapper.selectById(mobile)时必然触发空指针——这才是问题根源,和LoginController里的userService注入无关(加@Autowired后userService已经被正确注入了)。
解决:给UserMapper加上@Autowired注解:
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { @Autowired private UserMapper userMapper; // 剩余代码不变 }
2. 验证userService注入有效性
可以在LoginController的doLogin方法开头加日志,确认userService是否真的被注入:
@RequestMapping("/doLogin") @ResponseBody public RespBean doLogin(LoginVo loginVo) { log.info("userService是否为空:{}", userService == null); return userService.doLogin(loginVo); }
如果日志输出false,说明userService注入正常,空指针完全来自未注入的userMapper。
3. 无效手机号无响应问题排查
- 检查
ValidatorUtil.isMobile方法的逻辑是否正确,确保能正确识别无效手机号 - 在无效手机号的分支加日志,确认代码是否走到了返回逻辑:
if (!ValidatorUtil.isMobile(mobile)) { log.info("无效手机号:{}", mobile); return RespBean.error(RespBeanEnum.MOBILE_ERROR); }
- 确认
RespBean类是否正确实现了JSON序列化,保证前端能接收到返回值
内容的提问来源于stack exchange,提问作者meimaymay
相关产品推荐
相关产品推荐

