Spring 3 Request processing failed; nested excepti

2020-03-08 07:21发布

问题:

I am having difficulties while trying to launch my app, i looked for my mistake for a couple of days but i am stuck somewhere in the code and asking for your assistance Thanks

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>

when I try to navigate for instance /disableUser/Tugrul I get the following error:

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.

I am using 3.2.6.RELEASE version

回答1:

After a long research I came up with the solution towards my issue the below the solution is

Controller

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

SingleTransactionsServiceImpl

@Service("singleTransactionsService")

Normally the answer was supposed to be the @Autowired annotation but it did not work on me although I used @Qualifier and named too. Hope the solution helps someone else in the future too



回答2:

This field

private SingleTransactionsService singleTransactionsService;

is going to remain null, you aren't initializing it anywhere.

I believe you meant to inject a value into it

@Autowired
private SingleTransactionsService singleTransactionsService;