老师为什么我访问首页会空指针异常?
package com.mason.hospital.controller; import com.mason.hospital.entity.Category; import com.mason.hospital.entity.Department; import com.mason.hospital.service.CategoryService; import com.mason.hospital.service.DepartmentService; import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet(urlPatterns = "/index.do") public class IndexController extends HttpServlet { @Autowired CategoryService categoryService; @Autowired DepartmentService departmentService; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Category> categories = categoryService.selectAll(); List<Department> departments = departmentService.selectAll(); System.out.println("分类:"+categories); System.out.println("部门:"+departments); req.setAttribute("CLIST",categories); req.setAttribute("DLIST",departments); req.getRequestDispatcher("/hospital_detail.jsp").forward(req,resp); } }
29-Jul-2019 16:04:43.424 涓ラ噸 [http-nio-8080-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.mason.hospital.controller.IndexController] in context with path [/hospital] threw exception java.lang.NullPointerException at com.mason.hospital.controller.IndexController.service(IndexController.java:25) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at com.mason.hospital.global.EncodingFilter.doFilter(EncodingFilter.java:16) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
我在后台页面都是正常能获取到分类和部门的数据的
3
收起
正在回答
1回答
同学你好。这是由于IndexController并没有被Spring管理,所以categoryService不能直接使用
1、同学的类虽然是IndexController,但实际仍然是Servlet的写法。@WebServlet注解用于标注在一个继承了HttpServlet类之上,属于类级别的注解。该注解的作用等价于在web.xml中配置该servlet的<servlet-mapping>元素中的<url-pattern>。
2、课程视频中,老师只实现了一个Servlet,即DispatcherServlet,将路径做了一些处理,然后匹配上不同的Controller来进行处理。在Controller中才使用了注入。建议同学采用老师的方式使用。
如果解答了同学的疑问,望采纳~
祝学习愉快~
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星