我的Spring Boot程序正在编译,没有任何问题,但是每当我单击注册链接时,它都会引发NullPointerException错误。我真的不知道是什么原因造成的。错误如下:-
java.lang.NullPointerException: null
at com.concretepage.controller.UserInfoController.registration(UserInfoController.java:32) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_171]
UserinfoController类如下:-
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.concretepage.entity.ProjectLevel;
import com.concretepage.service.LevelService;
import com.concretepage.service.UserInfoService;
@Controller
@RequestMapping("app")
public class UserInfoController {
@Autowired
private UserInfoService userInfoService;
private LevelService levelService;
@GetMapping("login")
public ModelAndView login() {
ModelAndView mav = new ModelAndView();
mav.setViewName("custom-login");
return mav;
}
@GetMapping("registration")
public ModelAndView registration() {
ModelAndView mav = new ModelAndView();
List<ProjectLevel> levels = levelService.getAllLevels();
mav.addObject("Levels", levels);
mav.setViewName("registration");
return mav;
}
@GetMapping("secure/project-details")
public ModelAndView getAllUserProjects() {
ModelAndView mav = new ModelAndView();
mav.addObject("userProjects", userInfoService.getAllUserProjects());
mav.setViewName("projects");
return mav;
}
错误就在这行-
List<ProjectLevel> levels = levelService.getAllLevels();
LevelServiceImpl类如下:-
package com.concretepage.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.concretepage.entity.ProjectLevel;
import com.concretepage.repositories.LevelRepository;
@Service
public class LevelServiceImpl implements LevelService {
@Autowired
private LevelRepository levelRepository;
@Override
public List<ProjectLevel> getAllLevels() {
List<ProjectLevel> levelList = levelRepository.findAll();
return levelList;
}
}
答案 0 :(得分:4)
只需更改这段代码:
@Autowired
private UserInfoService userInfoService;
private LevelService levelService;
到
@Autowired
private UserInfoService userInfoService;
@Autowired
private LevelService levelService;
答案 1 :(得分:-1)
对于私有LevelService levelService,您必须使用单独的@Autowired;