我在保存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);
}
}
&#34;模型&#34;定义如下:
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中我在哪里获得Null指针,在序列化之前是否必须更新模型?
答案 0 :(得分:1)
好的,我发现了我的问题。您需要在序列化之前从表中分离模型,否则它将失败。所以Serializing现在看起来像这样:
[...]
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(简短,自包含,正确示例)方法,这引出了我发布:Loading Java JTable: Why does it not work?