I am trying to populate a JTable object, but when the instruction
private final DefaultTableModel tarifModel = (DefaultTableModel) tbTarife.getModel();
is called, I am getting a NullPointerException.
I am using Netbeans 8.2 and the GUI Builder plugin to create the JTable.
I've already implemented the part described here and any other question reagarding this problem seems to be not applicable.
My code:
public class TarifDialog extends javax.swing.JPanel {
private final TarifParser tP;
public TarifDialog() {
initComponents();
tP = new TarifParser(this);
}
//Generated code
private void initComponents() {
tbTarife = new javax.swing.JTable();
scrollpTarife = new javax.swing.JScrollPane();
tbTarife.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Schlüssel", "Name", "Beschreibung", "Preis", "Typ"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
scrollpTarife.setViewportView(tbTarife);
}
private javax.swing.JScrollPane scrollpTarife;
private javax.swing.JTable tbTarife;
//End generated code
private final DefaultTableModel tarifModel = (DefaultTableModel) tbTarife.getModel();
public void addTarifToTable(String Key, String name, String description, double price, String type) {
tarifModel.addRow(new Object[]{Key, name, description, price, type});
}
}
And the class that accesses it:
public final class TarifParser {
private final TarifVerwaltung tV;
private final TarifDialog tD;
public TarifParser(TarifDialog tD) {
this.tV = Init.getTarifVerwaltung();
this.tD = tD;
populateTarifTable();
}
public void populateTarifTable() {
for (HashMap.Entry<String, Tarif> tariff : tV.getAllTariffs().entrySet()) {
String type = "Undefined";
if (tariff.getValue().getType() == 0) {
type = "Standard Tarif";
} else if (tariff.getValue().getType() == 1) {
type = "Wiederkehrender Tarif";
} else if (tariff.getValue().getType() == 2) {
type = "Einmaliger Tarif";
}
tD.addTarifToTable(tariff.getKey(), tariff.getValue().getName(), tariff.getValue().getDescription(), tariff.getValue().getPrice(), type);
}
}
}