I created a JFrame containing the following components: a JLabel (just a simple text label), a JComboBox and a JButton. This JFrame reads from a config file after initializing it and the entries of the config file will be copied to a HashMap. I have a button to put further "local" entries into the HashMap (so the config file won't be touched after reading from it once).
My problem is that the delete button I have for every row I'm adding just works fine when I initialize the frame, and then delete directly some entries. After calling DISPOSE and reinitialize the frame the delete function throws some NullPointerException. The frameContainer (my JFrame) doesn't seem to find the components anymore (though they has been added again after reinitializing!)
Here's some code:
/**
* The standard constructor.
*
* @param extensionModel The model of the ModifyXESHeader view.
*/
public ModifyXESHeaderExtensions(ModifyXESHeaderModel extensionModel) {
super("Modify Header of XES File - Extensions");
this.frameContainer = new JFrame();
this.frameContainer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.frameContainer.setLayout(new GridLayout(0,3));
this.headerModel = extensionModel;
if (this.headerModel.componentExtensionMap.isEmpty()) {
this.count = 0;
} else {
this.count = this.headerModel.componentExtensionMap.size();
}
this.labelName = "label";
this.comboBoxName = "comboBox";
this.deleteButtonName = "deleteButton";
this.comboBoxItems = new String[]{"Concept.concept", "Lifecycle.lifecycle", "Organizational.org", "Time.time", "Semantic.semantic", "ID.identity", "Cost.cost"};
this.comboBoxItemsList = new ArrayList<String>();
addAllItemsToList(this.comboBoxItems);
this.addExtensionButton = new JButton("Add extension");
this.cancelButton = new JButton("Cancel");
this.emptyLabel = new JLabel();
this.frameContainer.add(this.addExtensionButton);
this.frameContainer.add(this.cancelButton);
this.frameContainer.add(this.emptyLabel);
//initialize the frame based upon the entries in the fileconfig.
initializeFrame();
this.frameContainer.revalidate();
this.frameContainer.repaint();
//pack'n'show
this.frameContainer.pack();
this.frameContainer.setLocationRelativeTo(null);
this.frameContainer.setVisible(true);
}
/**
* initializes the frame.
*/
public void initializeFrame() {
//for re-initializing the frame
if (!this.headerModel.componentExtensionMap.isEmpty()) {
for (int i = 1; i <= (this.headerModel.componentExtensionMap.size()); i++) {
if (this.headerModel.componentExtensionMap.get(this.comboBoxName + i) != null) {
this.extensionNamePrefixLabel = (JLabel) this.headerModel.componentExtensionMap.get(this.labelName + i);
this.extensionComboBox = (JComboBox) this.headerModel.componentExtensionMap.get(this.comboBoxName + i);
this.deleteButton = (JButton) this.headerModel.componentExtensionMap.get(this.deleteButtonName + i);
this.frameContainer.add(this.extensionNamePrefixLabel);
this.frameContainer.add(this.extensionComboBox);
this.deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteRow(e);
}
});
this.frameContainer.add(this.deleteButton);
this.frameContainer.revalidate();
this.frameContainer.repaint();
this.frameContainer.pack();
}
}
} else {
//Proof whether the config contains extension keys
Iterator<String> iterator = this.headerModel.fileConfig.getKeys();
while (iterator.hasNext()) {
String key = iterator.next();
String props = (String) this.headerModel.fileConfig.getProperty(key);
String[] tempStringArray = null;
if (!props.contains(":")) {
continue;
} else {
tempStringArray = props.split(":");
}
String selectedItem = null;
for (String listItem : this.comboBoxItemsList) {
System.out.println("Item: " + listItem);
if (listItem.contains(tempStringArray[0])) {
System.out.println("true");
selectedItem = listItem;
break;
}
}
if (this.comboBoxItemsList.contains(selectedItem)) {
++this.count;
this.extensionNamePrefixLabel = new JLabel("Extension Name.Prefix");
this.extensionComboBox = new JComboBox(this.comboBoxItems);
this.extensionComboBox.setSelectedItem(selectedItem);
this.deleteButton = new JButton("Delete");
this.deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteRow(e);
}
});
this.frameContainer.add(this.extensionNamePrefixLabel);
this.frameContainer.add(this.extensionComboBox);
this.frameContainer.add(this.deleteButton);
this.frameContainer.revalidate();
this.frameContainer.repaint();
this.frameContainer.pack();
this.headerModel.addExtensionComponentToMap(this.labelName, this.extensionNamePrefixLabel, this.count);
this.headerModel.addExtensionComponentToMap(this.comboBoxName, this.extensionComboBox, this.count);
this.headerModel.addExtensionComponentToMap(this.deleteButtonName, this.deleteButton, this.count);
}
}
}
}
And in this function the NullPointer will be thrown (at the place where frameContainer wants to delete the first component):
/**
* Deletes a row of the view.
*
* @param e ActionEvent.
*/
public void deleteRow(ActionEvent e) {
Integer buttonNumber = this.headerModel.indexExtensionMap.get(e.getSource());
JLabel tempLabel = (JLabel) this.headerModel.componentExtensionMap.get(this.labelName + buttonNumber);
JComboBox tempBox = (JComboBox) this.headerModel.componentExtensionMap.get(this.comboBoxName + buttonNumber);
JButton tempButton = (JButton) this.headerModel.componentExtensionMap.get(this.deleteButtonName + buttonNumber);
this.frameContainer.remove(tempLabel);
this.frameContainer.remove(tempBox);
this.frameContainer.remove(tempButton);
this.headerModel.deleteExtensionComponentFromMap(this.labelName + buttonNumber, tempLabel);
this.headerModel.deleteExtensionComponentFromMap(this.comboBoxName + buttonNumber, tempBox);
this.headerModel.deleteExtensionComponentFromMap(this.deleteButtonName + buttonNumber, tempButton);
this.frameContainer.revalidate();
this.frameContainer.repaint();
this.frameContainer.pack();
}
By the way: the components disappear from the GUI anyway.
FULL STACK TRACE:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.remove(Container.java:1259)
at javax.swing.JFrame.remove(JFrame.java:587)
at UI.ModifyXESHeaderExtensions.deleteRow(ModifyXESHeaderExtensions.java:211)
at UI.Controller.ModifyXESHeaderExtensionsController$1.actionPerformed(ModifyXESHeaderExtensionsController.java:59)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)`