1

I'm trying to create simple web app and getting error 500 when i entering my app in localhost.

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]

This is my Service class

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

My Controller

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

This is my DAO implementation

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

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

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>

And index.jsp which is laying down im my /WEB-INF/jsp directory

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

And link to my github profile. Maybe it will be easier to look this error there. Click here

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
shnaps
  • 53
  • 1
  • 2
  • 6
  • 1
    No my friend. This is another type of NullPointerException error in my cause. And idk how t fix it – shnaps Aug 24 '16 at 17:48
  • 1
    `` should be in your `dispatcher-servlet` configuration. – Sotirios Delimanolis Aug 24 '16 at 17:56
  • @Sotirios Still not working. I getting this error >org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'taskController': Unsatisfied dependency expressed through field 'taskService': No qualifying bean of type [tasker.service.TaskService] found for dependency [tasker.service.TaskService]: – shnaps Aug 24 '16 at 18:14
  • Please don't completely change your question after it's been answered. Ask a new question if your program doesn't work for other reasons. – Sotirios Delimanolis Aug 24 '16 at 23:18
  • @SotiriosDelimanolis Sorry, i still a newb here. But i think problem is still the same - beans aren't created. Should i wait for tomorrow and ask new question? – shnaps Aug 24 '16 at 23:21
  • You can. **But** this has been discussed a lot and a tutorial should help put you on the right path. Nothing in your application processes `applicationContext.xml`. You might want to read [this](http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s). – Sotirios Delimanolis Aug 24 '16 at 23:25
  • @SotiriosDelimanolis Okey, thx. Can you set answer closed? – shnaps Aug 24 '16 at 23:32
  • @VyacheslavRavinsky If you think an answer post answers your question, you can accept it (green tick next to score). If you think your question should be closed, you can vote for that under the post itself (next to your profile signature). You can also delete it if you feel like it. – Sotirios Delimanolis Aug 24 '16 at 23:33

2 Answers2

2

You need to debug public String showMain(Model model) set a break point in the first line, and inspect taskService for null, if that's the case then you should check you spring configuration, are you getting the autowiring right?

alibttb
  • 526
  • 4
  • 19
1

The stripped down version of your problem no longer has the NullPointerException in it. Sotirios Delimanolis's answer is the correct answer, and it's quite logical since Spring MVC beans should belong to the specific context attached to the dispatcher servlet.

So I went to your Github repo and, in your web.xml, I saw this:

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

This actually prevents the ContextLoaderListener from loading the default applicationContext.xml file. Thus, your application ends up with no services and no DAOs and a NullPointerException in TaskController. You will have to remove the context-param from web.xml and move <mvc:annotation-driven /> from applicationContext.xml to dispatcher-servlet.xml to solve the problem.

Arthur Noseda
  • 2,534
  • 19
  • 28
  • Thx for help! Updated all my code. But still got same error Unsatisfied dependency expressed through field 'taskService': No qualifying bean of type [tasker.service.TaskService] found for dependency [tasker.service.TaskService]. Can you tell me where is problem? :( – shnaps Aug 24 '16 at 22:48