2
當以下類被分離到不同的包中時,空指針:java.lang.NullPointerException: Cannot invoke method addPropertyChangeListener() on null object.
然而,將它們組合到一個包中是有效的。PropertyChangeListener和多態性不一致(與包)
包裝parents
:
package parents
import groovy.beans.Bindable
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
@Bindable
class GrandParent<T extends Parent> implements PropertyChangeListener {
String pets
GrandParent() {
this.addPropertyChangeListener(this)
}
@Override
void propertyChange(PropertyChangeEvent evt) {
println "prop change -> $evt"
}
}
class Parent extends GrandParent {}
包children
package children
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import groovy.beans.Bindable
import parents.*
class Child extends Parent {
@Bindable
String brothers
static main(args) {
Child child = new Child()
child.pets = "Leo, Sophie"
child.brothers = "Douglas, Ted"
}
}
把他們都在一個封裝/文件(註釋掉import parents.*
),你會發現它的工作原理,印刷:
> prop change -> java.beans.PropertyChangeEvent[propertyName=pets; oldValue=null; newValue=Leo, Sophie; propagationId=null; [email protected]]
> prop change -> java.beans.PropertyChangeEvent[propertyName=brothers; oldValue=null; newValue=Douglas, Ted; propagationId=null; [email protected]]
這是爲什麼?
工作正常。指定軟件包時,您是否將類放入相應的子目錄? – Steinar 2014-09-27 10:37:32
我把它們作爲兩個eclipse項目:一個用於父類和祖父類,另一個用於子類(引用父項目)。可以肯定的是,在您發表評論後,我確認我的同事也看到了同樣的問題(如果他們使用了兩個不同的項目)。 – inyourcorner 2014-09-30 15:12:53
奇怪...... @Steinar事實證明,它是否在同一個項目中並不重要。我發現有必要將''添加到'Parent'類定義中,如下所示: public class Parent extends GrandParent { 通過該更改,不再有空指針...(?) –
inyourcorner
2014-09-30 18:29:57