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

标签 java events nullpointerexception listener

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

项目面板:

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() {
    ....
}

关于java - 空指针异常 - 事件和监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34029969/

相关文章:

Java:One-jar Maven 插件资源

wpf - 使用 MVVM 从事件触发动画

javascript - 为页面上的每个 iFrame 添加 `load` 事件

android - 在 android 中运行模拟器期间出现内部错误

nullpointerexception - 使用 eclipse 运行 TestNg 测试组时出现 NPE 错误

java - 如何消除 Java OutOfMemoryError

java:如何停止等待很长时间的线程

java - 没有默认构造函数

python - 如何将参数传递给 tkinter 中的事件处理程序?

java - 如何删除空指针异常