空指针异常 - 事件和监听器

Null Pointer Exception - Events and Listeners

我试图将某些本地数据成员(在 ItemPanel 中)的值传递给不同的 class (MainFrame),以便调用 MainFrame 中的函数。为此,我使用了一个事件和一个 EventListener。我收到空指针异常,我不确定原因。任何帮助将不胜感激。谢谢。

物品面板:

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


}

您在设置侦听器之前忘记初始化 itemPanel:

MainFrame.java

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