Java 调度程序servlet中的空指针异常

Java 调度程序servlet中的空指针异常,java,spring,hibernate,Java,Spring,Hibernate,我正在尝试创建简单的web应用程序,在localhost中输入应用程序时出现错误500 20:14:32 ERROR Servlet.service() for servlet [dispatcher] in context with path [/tasker] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause j

我正在尝试创建简单的web应用程序,在localhost中输入应用程序时出现错误500

20:14:32 ERROR Servlet.service() for servlet [dispatcher] in context with path [/tasker] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
    java.lang.NullPointerException: null
        at controller.TaskController.showMain(TaskController.java:24) ~[main/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_92]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_92]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.33.jar:8.0.33]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92]
这是我的服务课

package service;
import dao.TaskDao;
import entity.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TaskServiceImpl implements TaskService {

    private TaskDao taskDao;

    @Autowired
    public void setTaskDao(TaskDao taskDao) {
        this.taskDao = taskDao;
    }

    @Override
    @Transactional
    public boolean addTask(Task task) {
        taskDao.addTask(task);
        return true;
    }
}
我的控制器

package controller;

import entity.Priority;
import entity.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.TaskService;

@Controller
public class TaskController {
    @Autowired
    private TaskService taskService;



    @RequestMapping("/")
    public String showMain(Model model) {
        Task task = new Task();
        task.setName("shnaps");
        task.setDescription("Hi im shnaps");
        task.setPriority(Priority.LOW);
        taskService.addTask(task);
        model.addAttribute(task);
        return "index";
    }
}
这是我的DAO实现

package dao;
import entity.Task;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class TaskDaoImpl implements TaskDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public boolean addTask(Task task) {
        Session session = sessionFactory.getCurrentSession();
        session.save(task);
        return true;
    }
}
调度器servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <context:component-scan base-package="controller"/>

</beans>

应用程序上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="hibernateContext.xml"/>

    <mvc:annotation-driven/>

    <context:component-scan base-package="service"/>
    <context:component-scan base-package="dao"/>

</beans>

以及index.jsp,它正在设置im my/WEB-INF/jsp目录

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
Hello, ${task.name} lol
</body>
</html>

$Title$
您好,${task.name}lol
并链接到我的github配置文件。也许在那里查看这个错误会更容易。

您需要调试
公共字符串showMain(模型)
在第一行中设置断点,并检查
taskService
是否为
null
,如果是这种情况,那么您应该检查spring配置,您的自动布线正确吗

问题的简化版本不再包含
NullPointerException
。Sotirios Delimanolis的答案是正确的,而且非常符合逻辑,因为Spring MVC bean应该属于附加到dispatcher servlet的特定上下文

所以我去了你们的Github回购,在你们的
web.xml
中,我看到了:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value></param-value>
</context-param>

上下文配置位置

这实际上阻止了
ContextLoaderListener
加载默认的
applicationContext.xml
文件。因此,应用程序最终没有服务和DAO,并且在
TaskController
中出现
NullPointerException
。您必须从
web.xml
中删除
上下文参数
,并将
applicationContext.xml
移动到
dispatcher servlet.xml
才能解决此问题。

不,我的朋友。这是我的原因中的另一种类型的NullPointerException错误。idk how t fix it
应该在您的
dispatcher servlet
配置中。@Sotirios仍然不工作。我收到此错误>org.springframework.beans.factory.unsatifiedpendencyException:创建名为“taskController”的bean时出错:通过字段“taskService”表示的未满足的依赖项:未找到依赖项[tasker.service.taskService]类型的合格bean当前位置回答问题后,请不要完全改变您的问题。如果你的程序因其他原因无法运行,请问一个新问题。@SotiriosDelimanolis抱歉,我还是个新手。但我认为问题仍然是一样的——豆子并没有被创造出来。我应该等到明天再问新问题吗?为什么这次投票被否决了?谁知道呢。我没有投票支持这个答案。这是针对新来者的:(这应该是一个评论。既然你是新来的,请阅读以了解如何开始处理堆栈溢出。你是对的,但我不能评论,因为我需要50次重复,现在你给我2分,我只是想加入:)Thx寻求帮助!更新了我所有的代码。但仍然得到了通过字段“taskService”表示的未满足依赖项的相同错误:未找到依赖项[tasker.service.taskService]类型为[tasker.service.taskService]的合格bean。你能告诉我哪里出了问题吗(