簡體   English   中英

Spring框架空指針異常

[英]spring framework null pointer exception

嘗試使用屬性自動裝配Spring bean,但仍然獲得NPE。 片段:

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

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

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>

主要

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

運行上面的代碼后,我得到列出的錯誤消息,並且當我將方法更改為static時

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

在第2課中,我得到了所需的輸出。 為什么?

基本上,SadFortune成員仍然為null,這就是為什么將getSadFortune()方法設為靜態時它可以工作的原因。

到目前為止,您只告訴Spring實例化一個Fortune bean和一個KabadiCoach,但是您必須告訴Spring KabadiCoach中的某些成員也需要自動裝配。

在您的Spring配置文件中嘗試以下操作:

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

編輯:對不起,覆蓋了autowire="byName"屬性。 在這種情況下,您可能只用小寫字母寫了名字。

<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