iteye_14491 2009-01-20 15:22
浏览 566
已采纳

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条回答 默认 最新

  • Candyut 2009-01-20 17:42
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!