Spring Web 应用程序 - java.lang.NullPointerException 与 jdbcTemplate

发布于 2024-12-10 19:33:39 字数 8150 浏览 0 评论 0原文

我正在使用 jdbcTemplate 编写一个简单的 spring 3.0 Restful Webservice 但每次我尝试访问资源时都会收到 java.lang.NullPointerException 。

我创建了一个像这样的 DAO

public interface MisCodeDao {

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

我的 DAOImpl 看起来像这样

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


}

这是我的 application-context.xml 的样子

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

完整的堆栈 strace 在这里:

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)

这是我的控制器:

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

我的 web.xml 看起来像这样

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-12-17 19:33:40

我可能在你的帖子中遗漏了一些东西...但是这里是...

  1. 控制器是基于注释的,请在应用程序上下文文件中向 spring 指示相同的内容。.还可以使用

  2. 而不是使用

private MisCodeDAOImp MiscodeImpl = new MisCodeDAOImp( );

使用以下内容进行自动装配

@Autowire
private MisCodeDAOImp miscodeImpl;

由于您已经在应用程序上下文文件中定义了 bean,因此它应该自动装配。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文