Guten Tag liebe Leute,
beim Ausführen meines Codes erhalte ich durchweg eine Nullpointer-Exception und eine Tabelle wird trotz einfachster return-Werte auch nicht gezeichnet. Kann mir jemand helfen?
Klasse Gui
JFrame frame;
JTable table;
TableModel dm;
public void initFrame(){
frame = new JFrame();
frame.setTitle("Titel");
frame.setSize(300,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addTable(){
dm = new MTableModel();
table = new JTable(dm);
frame.add(new JScrollPane(table));
}
public JFrame getMainFrame(){
return frame;
}
}```
Klasse MTableModel
```public class MTableModel implements TableModel {
private String[][] data = {{"Mein","Haus"},{"Dein", "Haustier"},{"Eine", "Vase"},{"Ein", "Gegenstand"}};
private TableModelListener l = null;
public MTableModel() {
l = new MTableListener();
}
@Override
public void addTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub
System.out.println("done1");
}
@Override
public Class<?> getColumnClass(int columnIndex) {
// TODO Auto-generated method stub
System.out.println("done2");
return null;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
System.out.println("done3");
return 1;
}
@Override
public String getColumnName(int columnIndex) {
// TODO Auto-generated method stub
System.out.println("done4");
return "Wort";
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
System.out.println("done5");
return 20;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
System.out.println("done6");
return "Object";
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
System.out.println("done7");
return false;
}
@Override
public void removeTableModelListener(TableModelListener l) {
l = null;
System.out.println("done8");
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
System.out.println("done9");
}
}
Klasse StartAppl
public static void main(String[] args) {
Gui gui = new Gui();
gui.initFrame();
gui.addTable();
gui.getMainFrame().setVisible(true);
}
}```
Der Errorcode:
```done1
done3
done4
done3
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done5
done2
done6
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
done5
done5
done5
done5
done5
done5
done5
Die Exception fliegt nur, wenn getColumnCount ein int >0 zurückgibt. Unabhängig vom Rückgabewert wird keine Tabell gezeichnet. Woran kann das liegen? die Mathode sorgt doch nur dafür, dass die Spaltenanzehl der Tabelle definiert wird.
Beste Grüße