2015-12-01 25 views
0

我試圖將一些本地數據成員(在ItemPanel中)的值傳遞給另一個類(MainFrame)以調用MainFrame中的函數。爲此,我使用Event和EventListener。我收到一個空指針異常,我不確定爲什麼。任何幫助將不勝感激。謝謝。空指針異常 - 事件和監聽器

ItemPanel:

public class ItemPanel extends JPanel { 

private boolean logged; 
private String item; 
private String buyPrice; 
private String sellPrice; 
private String quantity; 
private String pcBuyPrice; 
private String pcSellPrice; 

private ItemPanelLogListener itemPanelLogListener; 

private JButton logBtn; 

public ItemPanel(boolean logged, String item, String buyPrice, String sellPrice, 
     String quantity, String pcBuyPrice, String pcSellPrice) { 
    this.logged = logged; 
    this.item = item; 
    this.buyPrice = buyPrice; 
    this.sellPrice = sellPrice; 
    this.quantity = quantity; 
    this.pcBuyPrice = pcBuyPrice; 
    this.pcSellPrice = pcSellPrice; 

    Dimension dim = getPreferredSize(); 
    dim.height = 100; 
    setPreferredSize(dim); 
    dim.width = 900; 
    setMinimumSize(dim); 


logBtn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 



      ItemPanelLogEvent ev = new ItemPanelLogEvent(this, getLogged(), getItem(), getBuyPrice(), 
        getSellPrice(), getQuantity(), getPCBuyPrice(), getPCSellPrice()); 

      if (itemPanelLogListener != null) { 
       itemPanelLogListener.ItemPanelLogEventOccurred(ev); 
      } 

      System.out.println("Logged."); 

      // Remove after Logged 
      Container greatgrandparent = cancelBtn.getParent().getParent().getParent(); 
      Container grandparent = cancelBtn.getParent().getParent(); 
      greatgrandparent.remove(grandparent); 
      System.out.println("Removed due to ItemPanel being logged."); 
      greatgrandparent.revalidate(); 
      greatgrandparent.repaint(); 
     } 
    }); 
} 
} 

ItemPanelLogListener:

import java.util.EventListener; 


public interface ItemPanelLogListener extends EventListener{ 

public void ItemPanelLogEventOccurred(ItemPanelLogEvent e); 

} 

ItemPanelLogEvent:

import java.util.EventObject; 


public class ItemPanelLogEvent extends EventObject{ 

private boolean logged; 
private String item; 
private String buyPrice; 
private String sellPrice; 
private String quantity; 
private String pcBuyPrice; 
private String pcSellPrice; 

public ItemPanelLogEvent(Object source) { 
    super(source); 
} 

public ItemPanelLogEvent(Object source, boolean logged, String item, String buyPrice, 
     String sellPrice, String quantity, String pcBuyPrice, 
     String pcSellPrice) { 
    super(source); 
    this.logged = logged; 
    this.item = item; 
    this.buyPrice = buyPrice; 
    this.sellPrice = sellPrice; 
    this.quantity = quantity; 
    this.pcBuyPrice = pcBuyPrice; 
    this.pcSellPrice = pcSellPrice; 
} 

public boolean getLogged() { 
    return logged; 
} 

public String getItem() { 
    return item; 
} 

public String getBuyPrice() { 
    return buyPrice; 
} 

public String getSellPrice() { 
    return sellPrice; 
} 

public String getQuantity() { 
    return quantity; 
} 

public String getPcBuyPrice() { 
    return pcBuyPrice; 
} 

public String getPcSellPrice() { 
    return pcSellPrice; 
} 

} 

大型機:

public class MainFrame extends JFrame { 

private ToolBar toolBar; 
private FlipFormPanel flipFormPanel; 
private LogFormPanel logFormPanel; 
private FlipPanel flipPanel; 
private LogPanel logPanel; 
private ItemPanel itemPanel; 

public MainFrame() { 
    super("Flipping Guidance"); 

    setSize(1258, 684); 
    Dimension dim = new Dimension(); 
    dim.width = 428; 
    dim.height = 428; 
    setMinimumSize(dim); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    setJMenuBar(createMenuBar()); 

    toolBar = new ToolBar(); 
    flipFormPanel = new FlipFormPanel(); 
    logFormPanel = new LogFormPanel(); 
    flipPanel = new FlipPanel(); 
    logPanel = new LogPanel(); 

    // Item Panel Log Button 
    itemPanel.setItemPanelLogListener(new ItemPanelLogListener() { 
     public void ItemPanelLogEventOccurred(ItemPanelLogEvent e) { 
      boolean logged = e.getLogged(); 
      String item = e.getItem(); 
      String buyPrice = e.getBuyPrice(); 
      String sellPrice = e.getSellPrice(); 
      String quantity = e.getQuantity(); 
      String pcBuyPrice = e.getPcBuyPrice(); 
      String pcSellPrice = e.getPcSellPrice(); 

      log(logged, item, buyPrice, sellPrice, quantity, 
       pcBuyPrice, pcSellPrice); 
     } 
    }); 

    //Cancel 


} 
+1

在代碼中的哪一部分,你得到空指針異常?你能粘貼完整的錯誤嗎? – bstockwell

+0

請發佈(添加到您的問題)您的例外與完整的堆棧跟蹤。 –

回答

2

你忘了設置在其上的監聽器之前初始化itemPanel

MainFrame.java

itemPanel = new ItemPanel(....); 
itemPanel.setItemPanelLogListener(new ItemPanelLogListener() { 
    .... 
} 
+1

我這樣一個愚蠢的錯誤。謝謝@JDev –