iteye_14491
2009-01-20 15:22 阅读 456
已采纳

spring setter注入时报空指针错误的问题,希望高手路过看一下

报错信息
[code="java"]
java.lang.NullPointerException
at com.dareway.apps.demo.test.TestController.save(TestController.java:52)
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:585)
at com.dareway.apps.common.webcontroller.MultiActionController.invokeNamedMethod(MultiActionController.java:456)
at com.dareway.apps.common.webcontroller.MultiActionController.handleRequestInternal(MultiActionController.java:375)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.dareway.apps.common.webcontroller.SafetyFilter.doFilter(SafetyFilter.java:114)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.dareway.apps.common.webcontroller.EncodingFilter.doFilter(EncodingFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:595)
[/code]

web.xml中的配置

[code="java"]
....


org.springframework.web.context.request.RequestContextListener




org.springframework.web.context.ContextLoaderListener


....
[/code]

applicationContext.xml中的配置:
[code="java"]
....

<bean id="testDemo"
    class="com.dareway.apps.demo.test.TestDemo">
</bean>
<bean id="TestController"
    class="com.dareway.apps.demo.test.TestController">
    <property name="testDemo">
        <ref bean="testDemo" />
    </property>
</bean>

....
[/code]

TestDemo.java的内容:

[code="java"]
public class TestDemo implements TestInterface {

public void printDemo() {
    System.out.print("测试成功");
}

}
[/code]
TestInterface 的内容:

[code="java"]
public interface TestInterface {
public void printDemo();
}
[/code]

testController中的内容:

[code="java"]
public class TestController extends BizDispatchControler {

private  TestInterface testDemo;
public ModelAndView save(HttpServletRequest request,
        HttpServletResponse response, TestDTO dto) throws Exception {
    ...
//  WebApplicationContext applicationContext  = WebApplicationContextUtils
//       .getWebApplicationContext(request.getSession().getServletContext());
    //       TestDemo testDemo=(TestDemo) applicationContext.getBean("testDemo");//这样我就可以
     //                      得到非空的testdemo。但是这不是setter注入吧?
    testDemo.printDemo();//报空指针的地方

...
return null;

}

public void setTestDemo(TestInterface testDemo) {
    this.testDemo = testDemo;
}

}

[/code]

不知道这样配置到底是那个地方出了问题?

[b]问题补充:[/b]
[code="java"]
package com.dareway.apps.demo.test;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import com.dareway.apps.common.ClassHelper;
import com.dareway.apps.common.exception.ApplicationException;
import com.dareway.apps.common.log.BizLogHandler;
import com.dareway.apps.common.pub.util.DataObject;
import com.dareway.apps.common.pub.util.DataStore;
import com.dareway.apps.common.webcontroller.BizDispatchControler;
import com.dareway.apps.common.webcontroller.Delegator;

public class TestController extends BizDispatchControler {

private  TestInterface testDemo;
public ModelAndView enterTest(HttpServletRequest request,
        HttpServletResponse response, TestDTO dto) throws Exception {
    return new ModelAndView("test");
}

public ModelAndView query(HttpServletRequest request,
        HttpServletResponse response, TestDTO dto) throws Exception {
    DataObject pdo = new DataObject();
    DataStore vds = null;
    try {
        pdo.put("dto", dto);
        Delegator dele = new Delegator();
        DataObject result = dele.execute(
                "com.dareway.apps.demo.test.TestBPO", "query", pdo,
                getUser(request));// 用自己的bpo以及方法。
        vds = (DataStore) result.get("vds");
    } catch (ApplicationException e) {
        BizLogHandler.saveBizLog(request, e.getMessage());
        return this.showErrorMessage(e.getMessage());
    }
    return this.flashDataWindow(request, vds);
}

public ModelAndView save(HttpServletRequest request,
        HttpServletResponse response, TestDTO dto) throws Exception {
    DataObject pdo = new DataObject();
//  WebApplicationContext applicationContext  = WebApplicationContextUtils
//       .getWebApplicationContext(request.getSession().getServletContext());
    //TestDemo testDemo=(TestDemo) applicationContext.getBean("testDemo");
    testDemo.printDemo();

// try {
Collection entities = ClassHelper.getEntities(request,TestDTO.class,"testdatawindow3");
pdo.put("dto", dto);
pdo.put("entities", entities);
Delegator dele = new Delegator();

        return alertSuccessMessage("保存补贴成功。");

}

// public TestInterface getTestDemo() {
// return testDemo;
// }
//
public void setTestDemo(TestInterface testDemo) {
this.testDemo = testDemo;
}
}

[/code]

这是测试程序的源程序,我做了删改,所以行数可能不对。

我的问题是为什么注入不了。

报的问题就是testDemo=null
[b]问题补充:[/b]
[code="java"]

<bean id="testDemo"  
    class="com.dareway.apps.demo.test.TestDemo">   
</bean>   
<bean id="TestController"  
    class="com.dareway.apps.demo.test.TestController">   
    <property name="testDemo">   
        <ref bean="testDemo" />   
    </property>   
</bean>  

[/code]

这个是正确的,如果不正确的 在tomcat启动的应该就报错吧?
楼下的让我改成
class="com.dareway.apps.demo.test.TestInterface">

TestInterface是个接口 这样不对吧?

[b]问题补充:[/b]
你的controller不是由spring创建的。。。

它继承的是MultiActionController

这样不行吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

7条回答 默认 最新

  • 已采纳
    weixin_42513366 Candyut 2009-01-20 17:42

    不行的。必须是spring创建的。。。
    步骤是这样:
    1.spring创建controller实例。。。
    XXXController c = new XXXController();
    2.从你配置中得到你需要另一个实例注入
    TestDemo t = new TestDemo();
    c.setTestDemo(t);
    3.你的才能使用

    你的不是spring创建的,所以就没第一步,没第一步就没第二步。故你无法获得实例。

    点赞 评论 复制链接分享
  • laitaogood laitaogood 2009-01-20 15:27

    不是写了吗
    at com.dareway.apps.demo.test.TestController.save(TestController.java:52)

    点赞 评论 复制链接分享
  • weixin_42513366 Candyut 2009-01-20 15:43

    检查一下[code="xml"]

    <bean id="testDemo"

    class="com.dareway.apps.demo.test.TestDemo">

    <bean id="TestController"

    class="com.dareway.apps.demo.test.TestController">

    [/code]
    是否正确

    点赞 评论 复制链接分享
  • laitaogood laitaogood 2009-01-20 15:45

    [code="xml"] class="com.dareway.apps.demo.test.TestDemo">

    [/code]
    --->
    [code="xml"] class="com.dareway.apps.demo.test.TestDemo">

    [/code]

    点赞 评论 复制链接分享
  • laitaogood laitaogood 2009-01-20 15:46

    [code="xml"] class="com.dareway.apps.demo.test.TestInterface">

    [/code]

    点赞 评论 复制链接分享
  • weixin_42513366 Candyut 2009-01-20 16:59

    你的controller不是由spring创建的。。。
    所以你无法获得TestDemo的实例。

    你的配置都是对的。

    如果是struts2请参考相关配置。
    http://forjava.iteye.com/blog/129499

    点赞 评论 复制链接分享
  • bohemia bohemia 2009-01-20 17:45

    [quote]你的controller不是由spring创建的。。。

    它继承的是MultiActionController [/quote]

    呵呵.. 肯定,除非你,自己手工再注入.

    点赞 评论 复制链接分享

相关推荐