0

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

Tugrul ASLAN
  • 324
  • 1
  • 3
  • 13
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Apr 21 '18 at 17:22
  • The real location of the NullPointerException is further down in the stacktrace. The error log states, that the full stack trace can be found in the tomcat logs. There you'll find another stack trace under "Caused by". This can also happen, when you're writing a test case with mockMvc. It will wrap your code and in case of an exception, the outer stack trace can be quite misleading. The "Caused by" is what you want then. – lilalinux Jun 29 '20 at 13:15

3 Answers3

8

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

Tugrul ASLAN
  • 324
  • 1
  • 3
  • 13
  • 2
    The issue is that you have two `SingleTransactionsService` beans defined. One with `@Service` and `component-scan`, and one with an explicit `` declaration. Get rid of one and the `@Autowired` will work. – Sotirios Delimanolis Jan 24 '14 at 13:35
3

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;
Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
  • 2
    Hey thanks for the answer, but it was not the issue, for some reason as I digged info on the internet @ Autowired as supposed to be annotated above, and I sorted it out on my own with the @ Resource annotation and defined an exclusive name for it – Tugrul ASLAN Jan 24 '14 at 09:35
0

In my case, I tried to call the method from the null object (userFromDB.getPassword())

User userFromDB = userRepo.findByLogin(login);

boolean isPasswordMatch = userFromDB.getPassword().equals(password);