hte_guy hte_guy - 1 year ago 63
reST (reStructuredText) Question

NPE in my service class of rest using jersey with spring

I am new to spring and rest i am still learning it. I have a problem while calling the resource from my rest api through a client.

I have some data in my database and i am using spring to inject the values and get connection.

When i am making a GET request from the client i am getting a NPE.

Below is my DAO Class

public interface CustomerDao {

public List<Customer> getCustomersDao();



public class CustomerDaoImpl implements CustomerDao {

private JdbcTemplate template;

public void setTemplate(JdbcTemplate template) {
this.template = template;

public List<Customer> getCustomersDao() {
return template.query("SELECT * FROM books.customers", new RowMapper<Customer>() {
public Customer mapRow(ResultSet rs, int rownumber) throws SQLException {
Customer e = new Customer();
return e;

Service class

public class CustomerService {

CustomerDao customersConnection;

public List<Customer> getAllCustomers() {

return new ArrayList<Customer>(customersConnection.getCustomersDao());



public class CustomerResource {

CustomerService customerService;

public List<Customer> getAllCustomers {

return customerService.getAllCustomers();



My bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<context:component-scan base-package="com.sumanth.customers" />

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="--:--://----/---" />
<property name="username" value="----" />
<property name="password" value="-----" />

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>

<bean id="customerService" class="com.sumanth.customer.db.CustomerDaoImpl">
<property name="template" ref="jdbcTemplate"></property>



<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="" xmlns:xsi=""


<servlet-name>Jersey Web Application</servlet-name>






<servlet-name>Jersey Web Application</servlet-name>



Caused by: java.lang.NullPointerException
at com.sumanth.customer.resource.CustomerResource.getAllCustomers(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

Error line 23 :

return customerService.getAllCustomers();

I used the @Autowired to wire the beans.I know that i did a mistake while wiring them can anybody please help me out

Thank you

Answer Source

I have made some modifications to the web xml and my bean.xml


Modified my service class

   @Component annotation in my service class

My bean.xml added the below

<context:annotation-config />

My Impl class.I have added the annotation above my class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download