照书上的例子做的,结果运行时总是出以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
 at onlyfun.caterpillar.SpringDemo.main(SpringDemo.java:12)
Caused by: java.lang.NullPointerException
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:104)
 ... 1 more
程序代码如下:
HelloBean.java
package onlyfun.caterpillar; 
public class HelloBean { 
    private String helloWord; 
    
    public void setHelloWord(String helloWord) { 
        this.helloWord = helloWord; 
    }     public String getHelloWord() { 
        return helloWord; 
    } 
}
--------------------------------------
SpringDemo.java
package onlyfun.caterpillar; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; public class SpringDemo { 
    public static void main(String[] args) { 
        Resource rs = 
            new ClassPathResource("beans-config.xml"); 
        BeanFactory factory = new XmlBeanFactory(rs); 
        
        HelloBean hello = 
            (HelloBean) factory.getBean("helloBean"); 
        System.out.println(hello.getHelloWord()); 
    } 
}
-----------------------------------------------------
beans-config.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
       
    <bean id="helloBean" 
          class="onlyfun.caterpillar.HelloBean"> 
        <property name="helloWord">
            <value>Hello!Justin!</value>
        </property> 
    </bean> 
</beans>

解决方案 »

  1.   

    照书上的例子做的,结果运行时总是出以下错误:
    Exception in thread "main" java.lang.ExceptionInInitializerError
     at onlyfun.caterpillar.SpringDemo.main(SpringDemo.java:12)
    Caused by: java.lang.NullPointerException
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:104)
     ... 1 more
    程序代码如下:
    HelloBean.java
    package onlyfun.caterpillar; 
    public class HelloBean { 
      private String helloWord; 
       
      public void setHelloWord(String helloWord) { 
      this.helloWord = helloWord; 
      }   public String getHelloWord() { 
      return helloWord; 
      } 
    }
    --------------------------------------
    SpringDemo.java
    package onlyfun.caterpillar; import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; public class SpringDemo { 
      public static void main(String[] args) { 
      Resource rs = 
      new ClassPathResource("beans-config.xml"); 
      BeanFactory factory = new XmlBeanFactory(rs); 
       
      HelloBean hello = 
      (HelloBean) factory.getBean("helloBean"); 
      System.out.println(hello.getHelloWord()); 
      } 
    }
    -----------------------------------------------------
    beans-config.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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
       
      <bean id="helloBean" 
      class="onlyfun.caterpillar.HelloBean"> 
      <property name="helloWord">
      <value>Hello!Justin!</value>
      </property> 
      </bean> 
    </beans>
      

  2.   

    HelloBean hello =  
      (HelloBean) factory.getBean("helloBean");  
      System.out.println(hello.getHelloWord());  
    你的HelloWord没赋值,加一句hello.serHelloWord("dfdfd");
      

  3.   

    楼主使用下这个new ClassPathResource("beans-config.xml");  写绝对路径看下
      

  4.   

    楼主看下你的bean,就是哪个HELLO有没有拿到,再测你的里面内容有没拿到
      

  5.   

    ApplicationContext ac=new ClassPathXmlApplicationContext("beans-config.xml");
    HelloBean hello =   
      (HelloBean)ac.getBean("helloBean");
    楼主用这个测试下,如果不行再把问题贴出来看看
      

  6.   

    beans-config.xml  是否在classpath下面。因为没有 xml 解析报错,出现的nullpointexception 应该是发生在获取 beans-config文件出了问题。楼主检查下。
      

  7.   

    Spring了解甚少,还是了解其基本原理吧,~~