当前位置: 首页 > 知识库问答 >
问题:

"AWT-EventQueue-0"保存JTable(DefaultTableModel)时java.lang.NullPointerExcture

韩鸿波
2023-03-14

我在保存JTable(包含模型)时遇到问题。只要我只添加行和数据,简单地序列化模型就没有问题。当我删除一行并尝试保存时,会生成一个IO异常。我的代码:

此代码用于保存模型:

public static void saveModel() {
        if (isDatabasePathSet == true) {
            FileOutputStream fos = null;
            ObjectOutputStream out = null;

            try {
                table.clearSelection();
                table.repaint();
                if (table.isEditing()) { // prevent saving when user is editing a cell
                    table.getCellEditor().stopCellEditing();
                }

                fos = new FileOutputStream("test" + ".dbd");
                out = new ObjectOutputStream(fos);
                out.writeObject(model); // here I save the table contents
                out.writeObject(lfdNr); // this is just an increasing number / primary key
            } catch (Exception errWrite) {
                System.out.println("Fehler beim speichern der Datenbank");
            } finally {
                try {
                    fos.close();
                    out.close();
                } catch (Exception errClose) {
                    System.out.println("Fehler beim Schließen der Dateiströme");
                }
            }
        } else {
            JOptionPane
                    .showMessageDialog(
                            mainGui,
                            "<html>Es ist keine Datenbank ausgewählt, <br>bitte wählen Sie zunächst eine Datenbank im Menü aus: <br><br><i>Datenbank --> Datenbankpfad festlegen...</i></html>",
                            "Datenbankpfad fehlt", JOptionPane.WARNING_MESSAGE);
        }
    }

“模型”的定义如下:

model = new DefaultTableModel(new Object[][] {  },
    new String[] { "Lfd. Nr.", "FB Nr.", "Melde Nr.", "Betra Nr.", "Datum",
        "Ort", "Str. Km.", "Sprz.", "Mitarbeiter",
        "Auftrag / Objekt(e)", "Abgeschlossen" }) {

            private static final long serialVersionUID = 1L;
    Class[] columnTypes = new Class[] { Integer.class, String.class, Integer.class,
            Integer.class, String.class, String.class, String.class,
            String.class, String.class, String.class, Boolean.class };

    public Class getColumnClass(int columnIndex) {
        return columnTypes[columnIndex];
    }
};

我删除一行,如下所示:

JButton buttonDeleteTask = new JButton("Auftrag entfernen");
buttonDeleteTask.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int selectedRows[] = table.getSelectedRows();
        for (int i = 0; i < selectedRows.length; i++) {
            model.removeRow(selectedRows[i] - i);
        }
    }
});

这是我得到的例外:

Fehler beim speichern der Datenbank
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1761)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:779)
    at javax.swing.JComponent.paint(JComponent.java:1055)
    at javax.swing.JComponent.paintChildren(JComponent.java:888)
    at javax.swing.JComponent.paint(JComponent.java:1064)
    at javax.swing.JViewport.paint(JViewport.java:731)
    at javax.swing.JComponent.paintChildren(JComponent.java:888)
    at javax.swing.JComponent.paint(JComponent.java:1064)
    at javax.swing.JComponent.paintChildren(JComponent.java:888)
    at javax.swing.JSplitPane.paintChildren(JSplitPane.java:1047)
    at javax.swing.JComponent.paint(JComponent.java:1064)
    at javax.swing.JComponent.paintChildren(JComponent.java:888)
    at javax.swing.JComponent.paint(JComponent.java:1064)
    at javax.swing.JComponent.paintChildren(JComponent.java:888)
    at javax.swing.JComponent.paint(JComponent.java:1064)
    at javax.swing.JComponent.paintChildren(JComponent.java:888)
    at javax.swing.JComponent.paint(JComponent.java:1064)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:888)
    at javax.swing.JComponent.paint(JComponent.java:1064)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5232)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5180)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4991)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:738)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:699)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:708)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我看不出在AWT EventQue中从何处获得空指针,在序列化之前是否必须更新模型?

共有1个答案

邹野
2023-03-14

好吧我找到我的问题了。在序列化之前,您需要将模型从表中分离出来,否则它将失败。所以连载现在看起来是这样的:

[...]
    fos = new FileOutputStream("03032015.dbd");
                out = new ObjectOutputStream(fos);

                table.setModel(new DefaultTableModel()); // detach model from table while serializing to prevent it from failing
                out.writeObject(model);
                table.setModel(model);
[...]

保存后,您可以重新应用旧模型,没有任何问题。

多亏了DSquare和Andrew Thompson,我尝试了MCVE(最小完全可验证示例)或SSCCE(简短、自包含、正确示例)方法,这让我看到了这篇文章:加载JavaJTable:为什么它不工作?

 类似资料: