0

I am writing a simple spring 3.0 restful webservice with jdbcTemplate but i get a java.lang.NullPointerException each time i try to access a resource.

I have created a DAO like this

public interface MisCodeDao {

    public void setDataSource(DataSource ds);
//the remaining method declarations
    }
}

And my DAOImpl like this

public class MisCodeDAOImp implements MisCodeDao {
private JdbcTemplate jdbcTemplate;
    public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
public List<MisCode> findAll() throws MisCodeDAOException {
    if(this.jdbcTemplate==null)
    {
    System.out.print("JDBC TEMPLATE IS NULL");
    }
    return (List<MisCode>) this.jdbcTemplate.query("SELECT misCode, misClass, codeDesc, active from miscode", new RowMapper<MisCode>() {
    public MisCode mapRow(ResultSet resultSet, int row) throws SQLException {
    MisCode miscode = new MisCode();
    miscode.setMisCode(resultSet.getString("misCode"));
    System.out.print(resultSet.getString("misCode"));
    miscode.setMisClass(resultSet.getString("misClass"));
    miscode.setCodeDesc(resultSet.getString("codeDesc"));
    String charValueStr=resultSet.getString("active"); 
    miscode.setActive(charValueStr.charAt(0));
    return miscode;
        }
    });


}

Here is what my application-context.xml looks like

<?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
    http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driver}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}" />

<bean id="misCodeDAO" class="com.tavia.bacponline.DaoImpl.MisCodeDAOImp">
   <property name="dataSource" ref="dataSource"/>
</bean>
</beans>

The full stack strace is here:

WARNING: StandardWrapperValve[bacponline]: PWC1406: Servlet.service() for servlet bacponline threw exception
java.lang.NullPointerException
    at com.tavia.bacponline.DaoImpl.MisCodeDAOImp.findAll(MisCodeDAOImp.java:67)
    at com.tavia.bacponline.controller.MisCodeController.getMisCodes(MisCodeController.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:619)

Here is my controller:

@Controller
public class MisCodeController {
private Jaxb2Marshaller jaxb2Mashaller;
    public void setJaxb2Mashaller(Jaxb2Marshaller jaxb2Mashaller) {
    this.jaxb2Mashaller = jaxb2Mashaller;
}
private static final String XML_VIEW_NAME = "miscodes";
private MisCodeDAOImp miscodeImpl = new MisCodeDAOImp();

@RequestMapping(method=RequestMethod.GET, value="/miscodes")
public ModelAndView getMisCodes() throws MisCodeDAOException {
List<MisCode> miscodes = miscodeImpl.findAll();
MisCodeList list = new MisCodeList(miscodes);
return new ModelAndView(XML_VIEW_NAME, "miscodes", list);
}
}

And my web.xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>com.tavia.bacponline</display-name>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/bacponline-context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>bacponline</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>bacponline</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

Thanks

16
  • So, which line is line 67 of MisCodeDAOImp.java? Because as the stack trace says, that's the line which causes the NPE. Also, you already asked 11 questions and accepted no answer. Why would we help you? Commented Oct 19, 2011 at 17:06
  • @JB Nizet, i am really sorry but really i don't see this accept link except Was this post useful to you? i click that. I'm really sorry. here is the line return (List<MisCode>) this.jdbcTemplate.query("SELECT misCode, misClass, codeDesc, active from miscode", new RowMapper<MisCode>() { Commented Oct 19, 2011 at 17:15
  • Do you see the line JDBC TEMPLATE IS NULL in the output? Do you have any "caused by ... " after the stack trace? Commented Oct 19, 2011 at 17:22
  • @JB Nizet. yes i see JDBC TEMPLATE IS NULL Commented Oct 19, 2011 at 17:26
  • How do you get a reference to your DAO? What's the code you're using in MisCodeController to get your DAO? Commented Oct 19, 2011 at 17:35

1 Answer 1

1

I may be missing something in your post... but here goes...

  1. The Controller is an annotation based, please indicate the same to spring in the app context file.. also indicate the components to be scanned using

  2. Rather than using

private MisCodeDAOImp miscodeImpl = new MisCodeDAOImp();

use the following for autowiring

@Autowire
private MisCodeDAOImp miscodeImpl;

Since you have already defined the bean in app context file, it should autowire.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response but i do not really understand what point 1 says. Thanks
Thanks @Tom. I got it working by adding this line to my app-context.xml <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.