简体   繁体   English

Spring框架空指针异常

[英]spring framework null pointer exception

Trying to autowire Spring bean with property, but still getting NPE. 尝试使用属性自动装配Spring bean,但仍然获得NPE。 Snippets: 片段:

INFO: Loading XML bean definitions from class path resource [autoWireByName.xml]
Exception in thread "main" today do push-ups for 30 mins
java.lang.NullPointerException
    at com.springAutoWireByName.KabadiCoach.getFortune(KabadiCoach.java:23)
    at com.springAutoWireByName.AutoWireByName.main(AutoWireByName.java:13)

KabadiCoach.java KabadiCoach.java

package com.springAutoWireByName;

public class KabadiCoach {

    private SadFortune sadFortune;

    /*public KabadiCoach(){

        System.out.println("inside default Constructor");

    }*/
    public String getDailyWorkout()
    {
        return "today do push-ups for 30 mins";
    }
    public void setSadFortune(SadFortune fortune) {

        sadFortune = fortune;
    }

    public String  getFortune() {       

        return sadFortune.getSadFortune();
    }
}

SadFortune.java SadFortune.java

package com.springAutoWireByName;

public class SadFortune {

    public  String getSadFortune()
    {
        System.out.println();
        return "your day wont be good enough Sorry!!!";
    }       
 }
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

<!-- bean definitions here -->

<bean name="Fortune" class="com.springAutoWireByName.SadFortune">
</bean>
<bean id="myCoach" class="com.springAutoWireByName.KabadiCoach" autowire="byName" />


<!-- this is just a prototype to define actual just make use of this file-->

</beans>

main 主要

package com.springAutoWireByName;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutoWireByName {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("autoWireByName.xml");

        KabadiCoach co = context.getBean("myCoach",KabadiCoach.class);
        System.out.println(co.getDailyWorkout());
        System.out.println(co.getFortune());
    }
}

after running the above code I am getting the error message as listed and when I change the method to static 运行上面的代码后,我得到列出的错误消息,并且当我将方法更改为static时

public static  String getSadFortune()
{
    System.out.println();
    return "your day wont be good enough Sorry!!!";
}

In class 2 I got the desired output. 在第2课中,我得到了所需的输出。 Why? 为什么?

Basically the SadFortune member is still null, which is why it works if the getSadFortune() method will be made static. 基本上,SadFortune成员仍然为null,这就是为什么将getSadFortune()方法设为静态时它可以工作的原因。

Until now you only tell Spring to instantiate a Fortune bean and a KabadiCoach, but you have to tell Spring that some members in the KabadiCoach need to be autowired too. 到目前为止,您只告诉Spring实例化一个Fortune bean和一个KabadiCoach,但是您必须告诉Spring KabadiCoach中的某些成员也需要自动装配。

Try this in your Spring configuration file: 在您的Spring配置文件中尝试以下操作:

<bean id="fortune" class="com.springAutoWireByName.SadFortune"></bean>
<bean id="myCoach" class="com.springAutoWireByName.KabadiCoach" autowire="byName">
    <property name="fortune" ref="fortune" />
</bean>

EDIT: Sorry, overread the autowire="byName" attribute. 编辑:对不起,覆盖了autowire="byName"属性。 In this case you probably just have write the name in lower case. 在这种情况下,您可能只用小写字母写了名字。

<bean id="fortune" class="com.springAutoWireByName.SadFortune"/>
<bean id="myCoach" class="com.springAutoWireByName.KabadiCoach" autowire="byName"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM