rockstar_77 rockstar_77 - 3 months ago 15
MySQL Question

Getting null pointer exception while getting from mysql db

Hello all i am using rest and mysql as my db.

Below is my rest get code for getting allbooks(Resource)

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getBooks(@QueryParam("format") String format) {

//line35: return Response.status(Status.OK).entity((new GenericEntity<List<Book>>(bookService.getAllBooks()) {
})).header(HttpHeaders.CONTENT_TYPE, "XML".equalsIgnoreCase(format)
? MediaType.APPLICATION_XML + ";charset=UTF-8" : MediaType.APPLICATION_JSON + ";charset=UTF-8").build();

}


Below is my service

public List<Book> getAllBooks() {

//books service line 24 return new ArrayList<Book>(booksDao.getAllBooks());
/* return new ArrayList<Book>(books.values()); */
}


BooksDAO class

public class BooksDAO {

private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<Book> getAllBooks() {
//books dao line24: return jdbcTemplate.query("select * from books.books_table", new RowMapper<Book>() {

@Override
public Book mapRow(ResultSet rs, int rownumber) throws SQLException {
Book e = new Book();
e.setId(rs.getInt(1));

e.setName(rs.getString(2));

e.setPrice(rs.getString(3));

e.setAuthor(rs.getString(4));

return e;
}


});
}


Below is my bean.I am using springJDBC

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

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

<bean id="edao" class="com.nag.library.database.BooksDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

</beans>


Without connecting to db i am fine getting the json/xml response but when i connect db its throwing a nullpointer exception

Aug 16, 2016 10:43:08 AM org.apache.catalina.core.StandardWrapperValve
invoke
SEVERE: Servlet.service() for servlet Jersey Web Application threw exception
java.lang.NullPointerException
at com.nag.library.database.BooksDAO.getAllBooks(BooksDAO.java:24)
at com.nag.library.service.BookService.getAllBooks(BookService.java:24)
at com.nag.library.resource.BookResource.getBooks(BookResource.java:35)
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)
at java.lang.Thread.run(Unknown Source)


Where i'm doing the thing wrong.Please guide me.

Thank you

Answer

I think that the problem is that your service is not a bean managed by Spring since not declared in your spring conf. So you don't use these beans :

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

<bean id="edao" class="com.nag.library.database.BooksDAO">  
<property name="jdbcTemplate" ref="jdbcTemplate"></property>  
</bean>  

And as consequence, your jdbcTemplace is null in your BooksDAOwhich you use at the runtime.
add your service as a bean in this configuration and add to it the bean edao as a property.
You should also wire your service with your rest class.

Edit for precision : I think that the problem is here because in this line return jdbcTemplate.query("‌​select * from books.books_table", new RowMapper<Book>() the NullPointerException can have a single cause : jdbcTemplate is null. Besides, you don't declare the service bean in his configuration. Besides, no instruction in the method body may trigger a NPE. –

Comments