2011-08-02 28 views
2

我已經實現了此方法來更改actionPerformed方法正在使用的PropertyChangeSupport的值。但是,由於PropertyChangeSupport實例是null,我遇到了NullPointerException。誰能告訴我這個問題?以下是代碼片段。Java中的addPropertyChangeListener方法

對於的PropertyChangeListener:

public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 
    if (pcs == null) { 
     pcs = new PropertyChangeSupport(this); 
    } 
    this.pcs.addPropertyChangeListener(listener); 
} 

對於事件:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    Task oldTask = this.task; 
    this.task = new TaskImpl(); 
    this.pcs.firePropertyChange(PROP_TASK, oldTask,this.task); 
    this.updateForm(); 
} 

回答

2

這可能是因爲您呼叫this.pcs.firePropertyChange(PROP_TASK, oldTask,this.task);你調用之前任何類實例化的PropertyChangeSupport(PCS)在其addPropertyChangeListener () 方法。即底層代碼在頂層(如果有的話)被調用之前被調用。您可以嘗試檢查jButtonActionPerformed()方法中的pcs是否爲null並在其中實例化。

0

似乎是在構造一個未接來電:

public TaskEditorPanel() { 
    if (null == this.taskMgr) { 
     this.taskMgr = Lookup.getDefault().lookup(TaskManager.class); 
    } 
    if (null != this.taskMgr) { 
     this.task = this.taskMgr.createTask(); 
    } 
    initComponents(); 
    this.updateForm(); 

    // missed call 
    this.pcs = new PropertyChangeSupport(this); 
}