Search code examples
javaspring-bootservletsnullpointerexceptionautowired

@Autowired, Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [...... java.lang.NullPointerException]


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.

https://www.baeldung.com/spring-autowire


Solution

    1. It is best to implement a bean through a constructor, so you can remove Autowired in the LoginController class and create a constructor with the necessary parameter, you can use Lombok
    2. since the code is not numbered, it is not clear in which line the npe falls out, but I can assume that in the mapper, you need to check that the service / component annotation is in the mapper class and also create a constructor with a mapper in the service class