1. ホーム
  2. spring-mvc

[解決済み] Spring 3 リクエスト処理に失敗しました。ネストされた例外は java.lang.NullPointerException です。

2022-02-14 11:02:40

質問

私は自分のアプリを起動しようとしているときに問題があります、私は数日間、私の間違いを探したが、私はコードのどこかで立ち往生しているとあなたの助けを求めている ありがとうございます。

SingleTransactionsController

@Controller
public class SingleTransactionsController {
private SingleTransactionsService singleTransactionsService;


@RequestMapping(value="/disableUser/{sicil}", method=RequestMethod.GET)
public String disableUser(@PathVariable String sicil, Model model){
    singleTransactionsService.disableUser(sicil);
    model.addAttribute("message", sicil);
    return "hello";
}

}

SingleTransactionsDAO

public interface SingleTransactionsDAO {

public void disableUser(String sicil);

}

SingleTransactionsDAOImpl

@Repository
public class SingleTransactionsDAOImpl implements SingleTransactionsDAO{

@Override
public void disableUser(String sicil) {
    System.out.println(sicil);
}
}

SingleTransactionsService

public interface SingleTransactionsService {
public void disableUser(String sicil);
}

SingleTransactionsServiceImpl

@Service
public class SingleTransactionsServiceImpl implements SingleTransactionsService{

@Autowired
private SingleTransactionsDAO singleTransactionsDAO;


public void disableUser(String sicil) {
    singleTransactionsDAO.disableUser(sicil);

}


public SingleTransactionsDAO getSingleTransactionsDAO() {
    return singleTransactionsDAO;
}


public void setSingleTransactionsDAO(SingleTransactionsDAO singleTransactionsDAO) {
    this.singleTransactionsDAO = singleTransactionsDAO;
}

mvc-dispatcher-servlet.xml。

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

<context:annotation-config />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<!-- Load only @Controller annotated controllers -->
<context:component-scan base-package="com.akbank.controller"
    use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller"
        type="annotation" />
</context:component-scan>
<mvc:annotation-driven />

        <bean     class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">    </bean>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<bean id="singleTransactionsDAO" class="com.akbank.dao.SingleTransactionsDAOImpl">    </bean>
<bean id="singleTransactionsService" class="com.akbank.service.SingleTransactionsServiceImpl"></bean>

例えば、/disableUser/Tugrulに移動しようとすると、以下のエラーが発生します。

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.jav    a:948)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.NullPointerException
com.akbank.controller.SingleTransactionsController.disableUser(SingleTransactionsCon    troller.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25    )
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandle    rMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(Invoc    ableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.    invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.i    nvokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.h    andleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(Abstr    actHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:    925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:8    56)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.jav    a:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.50     logs.

3.2.6.RELEASEバージョンを使用しています。

解決方法を教えてください。

長い研究の後、私は私の問題に対する解決策を考え出した、以下の解決策です。

コントローラ

@Resource(name = "singleTransactionsService")
private SingleTransactionsService singleTransactionsService;

SingleTransactionsServiceImpl

@Service("singleTransactionsService")

本来なら@Autowiredアノテーションで解決するはずなのですが、@Qualifierとnamedも使いましたが、うまくいきませんでした。この解決策も将来的に誰かの役に立つことを願っています。