2016-08-24 16 views
-1

私はlocalhostで自分のアプリケーションを入力すると、単純なWebアプリケーションを作成してエラー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] 

は、これはこれは私のDAOの実装である私のサービスクラス

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"; 
    } 
} 

ある

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; 
    } 
} 

ディスパッチャ・サーブレット

<?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> 

私のgithubのプロファイルにイム私の/ 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> 

そしてリンクを敷設しているのApplicationContext

<?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を。たぶん、このエラーを見やすいでしょう。あなたがpublic String showMain(Model model)は、最初の行にブレークポイントを設定し、nullためtaskServiceを検査、そのような場合、あなたはあなたが設定春確認する必要がありますデバッグする必要がある Click here

+1

私の友人はいません。これは私の原因で別のタイプのNullPointerExceptionエラーです。そして、それを修正する方法 –

+1

''はあなたの 'dispatcher-servlet'設定に含まれていなければなりません。 –

+0

@Sotiriosまだ動作しません。このエラーが発生する> org.springframework.beans.factory.UnsatisfiedDependencyException: 'taskController'という名前のBeanを作成中にエラーが発生しました: 'taskService'フィールドで表現された依存関係が満たされていません:[tasker.service.TaskService]タイプの適格なBeanが見つかりません[tasker .service.TaskService]: –

答えて

0

問題の削除されたバージョンには、NullPointerExceptionが含まれなくなりました。 Sotirios Delimanolisの答えは正しい答えです.Spin MVC Beanはディスパッチャーサーブレットに付属する特定のコンテキストに属している必要があるため、非常に論理的です。

だから私はあなたのGithubレポに行って、あなたのweb.xmlで、私はこれを見た:

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

これは、実際にデフォルトapplicationContext.xmlファイルをロードするからContextLoaderListenerを防ぐことができます。したがって、アプリケーションはサービスなしでDAOなしで終了し、TaskControllerにはNullPointerExceptionとなります。 からcontext-paramを削除し、applicationContext.xmlからdispatcher-servlet.xml<mvc:annotation-driven />を移動して問題を解決する必要があります。

+0

ヘルプのためのThx!私のすべてのコードを更新しました。しかし、それでも同じエラーが発生する 'taskService'フィールドで表される満足しない依存関係:[tasker.service.TaskService]タイプの適格なBeanが依存関係[tasker.service.TaskService]で見つかりません。どこに問題があるのか​​教えていただけますか? :( –

-1

、あなたはオートワイヤリング権利を得ていますか?

+0

なぜこれが投票されたのですか? – alibttb

+0

誰が知っていますか?私はこの答えに投票しませんでした。 –

+0

これは新しいコーラーに対するものです:( – alibttb