I'm trying to pass the values of some local data members (in ItemPanel) to a different class (MainFrame) in order to call a function in MainFrame. To do this, I'm using an Event and an EventListener. I am receiving a Null Pointer Exception and I am unsure why. Any help would be greatly appreciated. Thank you.
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;
}
}
MainFrame:
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
}