close

[Solved] Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception Error Occurs?

Today I am facing the following error Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception in Java. So

How To Solve Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception Error ?

  1. How To Solve Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception Error ?

    To Solve Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception Error Although you have defined the jdbcUserDAO bean, you have not wired it into the UserController class.

  2. Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception

    To Solve Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception Error Although you have defined the jdbcUserDAO bean, you have not wired it into the UserController class.

Solution 1

Although you have defined the jdbcUserDAO bean, you have not wired it into the UserController class. This causes a NullPointerException because the jdbcUserDAO field is null, which results in the exception when jdbcUserDAO.getAllUsers() is called. To solve this, annotate the jdbcUserDAO setter in the UserController with @Autowired, as in:

@RestController
public class UserController {

    private JdbcUserDAO jdbcUserDAO;

    public JdbcUserDAO getJdbcUserDAO() {
        return jdbcUserDAO;
    }

    @Autowired
    public void setJdbcUserDAO(JdbcUserDAO jdbcUserDAO) {
        this.jdbcUserDAO = jdbcUserDAO;
    }

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "Welcome to RestTemplate Example.";
    }

    @RequestMapping(value="/users", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody   
    public List<User> getAllUsers() {
        System.out.println("jdbcUserDAO"+jdbcUserDAO);
        List<User> userList = jdbcUserDAO.getAllUsers();
        System.out.println(userList);
        return userList;
    }

}

The @Autowired annotation tells Spring that a bean that matches the interface of the parameter for the setter (in this case, JdbcUserDAO) should be injected into the setter, which results in the jdbcUserDAO field being set to your JdbcUserDAO bean defined in application_config.xml.

You could also simplify your UserController and annotate the field (removing the getter and setter):

@RestController
public class UserController {

    @Autowired
    private JdbcUserDAO jdbcUserDAO;

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "Welcome to RestTemplate Example.";
    }

    @RequestMapping(value="/users", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody   
    public List<User> getAllUsers() {
        System.out.println("jdbcUserDAO"+jdbcUserDAO);
        List<User> userList = jdbcUserDAO.getAllUsers();
        System.out.println(userList);
        return userList;
    }

}

It also looks like your application_config.xml is not being included in Spring’s application context. To include this XML file, change your ApplicationConfiguration class to resemble the following:

@ComponentScan(basePackages = {"com.ganeshTrading.riceAppService"})
@Configuration
@EnableWebMvc
@ImportResource("classpath:/path/to/application_config.xml")
public class ApplicationConfiguration {

}

Note that classpath:/path/to/application_config.xml is an example and the actual location of your application_config.xml file should be used instead.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read