0

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)`
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
X-Fate
  • 323
  • 4
  • 13
  • The key, the most important factor to consider when debugging NullPointerExceptions (NPE's -- and surely you searched this before coming here, right?), and something that the stacktrace will tell you, are the lines involved that throw the exception -- so which lines are they? – Hovercraft Full Of Eels Feb 17 '15 at 17:38
  • what's the stack trace for the error – Marshall Tigerus Feb 17 '15 at 17:39
  • `this.frameContainer.remove(tempLabel)` throws the NullPointer after I reinitialize the frame. Nonetheless, I said this before. :) – X-Fate Feb 17 '15 at 17:39
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jens Feb 17 '15 at 17:40
  • @MarshallTigerus: I posted the stack trace above. – X-Fate Feb 17 '15 at 17:41
  • So what is null on that line? frameContainer? tempLabel? I have a sneaking suspicion that your overall program design could be possibly improved reducing the need for this code. For instance, are you throwing multiple JFrames at the user? If so, perhaps consider swapping views using a CardLayout instead, as this will result in a much more professional looking and acting GUI. – Hovercraft Full Of Eels Feb 17 '15 at 17:42
  • @HovercraftFullOfEels: well after reinitializing the frame, `frameContainer` is called again with `new JFrame()` and the map won't be deleted due to the fact that it is in another map (furhermore the integer `buttonNumber` is not null - according to the debugger - after getting the button from the map) – X-Fate Feb 17 '15 at 17:44
  • But again, what variable on that line is `null`? Something has to be`null` for the NPE to be thrown. – Hovercraft Full Of Eels Feb 17 '15 at 17:46
  • btw, unless there's a good reason to remove the objects, its about a billion times simpler just to hide their container. Likely you are removing something and its trying to reference the removed object shortly after (removing a container before removing its contents or something) – Marshall Tigerus Feb 17 '15 at 17:49
  • @HovercraftFullOfEels: I debugged it once again. Neither `frameContainer` nor `tempLabel` / `tempBox` / `tempButton` is `null`. – X-Fate Feb 17 '15 at 17:54
  • @X-Fate, then if still stuck, create and post a [minimal example program or mcve](http://stackoverflow.com/help/mcve), the smallest program possible, one that we can run and debug without modification, one that is small enough to post here in your original question, and that does nothing other than shows for us your problem. Yes, this will involve considerable effort on your part, but I fear that it will be the only way for us to be able to understand your problem well enough to be able to help you. Whatever you do, please don't post a link to your entire code base. – Hovercraft Full Of Eels Feb 17 '15 at 18:05

0 Answers0