title: "AWT-EventQueue-0" java.lang.NullPointerException when saving JTable (DefaultTableModel)
body: <p>I ran into trouble saving a <code>JTable</code> (containing the model). Simply serializing the model is no problem as far as I only add rows and Data. The moment where I remove one row and try to save generates an IO exception. My code:</p>
  
  <p><strong>This code is used to save the model:</strong></p>
  
  <pre><code>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,
                              "&lt;html&gt;Es ist keine Datenbank ausgewählt, &lt;br&gt;bitte wählen Sie zunächst eine Datenbank im Menü aus: &lt;br&gt;&lt;br&gt;&lt;i&gt;Datenbank --&gt; Datenbankpfad festlegen...&lt;/i&gt;&lt;/html&gt;",
                              "Datenbankpfad fehlt", JOptionPane.WARNING_MESSAGE);
          }
      }
  </code></pre>
  
  <p><strong>The "model" is defined like this:</strong></p>
  
  <pre><code>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];
      }
  };
  </code></pre>
  
  <p><strong>I delete a row like this:</strong></p>
  
  <pre><code>JButton buttonDeleteTask = new JButton("Auftrag entfernen");
  buttonDeleteTask.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          int selectedRows[] = table.getSelectedRows();
          for (int i = 0; i &lt; selectedRows.length; i++) {
              model.removeRow(selectedRows[i] - i);
          }
      }
  });
  </code></pre>
  
  <p><strong>This is the exception I get:</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I don't see where I get a Null Pointer in the AWT-EventQue, do I have to update the model before serializing it? </p>