老师为什么我访问首页会空指针异常?

老师为什么我访问首页会空指针异常?

http://img1.sycdn.imooc.com//climg/5d3ea90a0001a8eb05620532.jpg

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)

http://img1.sycdn.imooc.com//climg/5d3ea98d0001945807910470.jpg

http://img1.sycdn.imooc.com//climg/5d3ea9c30001488609610253.jpg

http://img1.sycdn.imooc.com//climg/5d3ea9c80001c37209650265.jpg

我在后台页面都是正常能获取到分类和部门的数据的

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好。这是由于IndexController并没有被Spring管理,所以categoryService不能直接使用

1、同学的类虽然是IndexController,但实际仍然是Servlet的写法。@WebServlet注解用于标注在一个继承了HttpServlet类之上,属于类级别的注解。该注解的作用等价于在web.xml中配置该servlet的<servlet-mapping>元素中的<url-pattern>。

2、课程视频中,老师只实现了一个Servlet,即DispatcherServlet,将路径做了一些处理,然后匹配上不同的Controller来进行处理。在Controller中才使用了注入。建议同学采用老师的方式使用。

如果解答了同学的疑问,望采纳~

祝学习愉快~



  • MasonM 提问者 #1
    老师,这样的话,那不就:前台模块跟后台模块都需要一个DispatcherServlet来对路径进行处理?
    2019-07-30 10:38:41
  • 芝芝兰兰 回复 提问者 MasonM #2
    同学你好。如果想要用Controller来进行处理,最好是和老师视频中一样,写一个DispatcherServlet来进行分发,否则失去了DispatcherServlet最初的设计意义。如果同学想要用Servlet的方式,则可以每个模块分开来写,但要注意Spring不能通过注解向Servlet中注入实例。祝学习愉快~
    2019-07-30 15:27:35
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师