springmvc报的service空指针的异常

2018-02-05 11:06:27来源:oschina作者:天佑我儿人点击

分享
今天在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

微信扫一扫

第七城市微信公众平台