springmvc报的service空指针的异常

时间:2022-10-27 13:10:31
今天在springmvc框架的配置中,出现了HTTP STATUS 500

Request processing failed; nested exception is java.lang.NullPointerException


具体服务器报错如下:
type Exception report

message Request processing failed; nested exception is java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause

错误原因为controller类中的service注入,在
@Controller
@RequestMapping("/courses")
public class CourseController  {
	@Autowired
	private CourseService courseService;

	public CourseService getCourseService() {
		return courseService;
	}
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	
	

	//本方法将处理 /courses/view?courseId=123 形式的URL
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public String viewCourse(@RequestParam("courseId") Integer courseId,
			Model model) {
		
		Course course=courseService.getCoursebyId(courseId);
	//	Log.debug("In viewCourse, courseId = {}", courseId);
				return "course_overview";
	}
@Autowired
	private CourseService courseService;

在service注入那里加上  @Autowired  注解

再次试验,空指针异常消失,service成功注入controller