删除按钮:重新初始化JFrame后出现NullPointerException

时间:2015-02-17 17:37:14

标签: java swing

我创建了一个包含以下组件的JFrameJLabel(只是一个简单的文字标签),JComboBoxJButton。初始化后,JFrame从配置文件中读取,配置文件的条目将复制到HashMap。我有一个按钮可以进一步放置" local" HashMap中的条目(因此配置文件在读取之后不会被触及)。

我的问题是,我添加的每一行的删除按钮在初始化帧时都能正常工作,然后直接删除一些条目。在调用DISPOSE并重新初始化帧之后,delete函数会抛出一些NullPointerException。 frameContainer(我的JFrame)似乎不再找到组件(虽然它们在重新初始化后又被添加了!)

这里有一些代码:

/**
 * 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);
            }
        }
    }
}

在这个函数中,将抛出NullPointer(在frameContainer想要删除第一个组件的地方):

/**
 * 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();
}

顺便说一句:无论如何,组件都会从GUI中消失。

完全堆积痕迹:

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)`

0 个答案:

没有答案