I'm making an invoice-program in Java. I'm making a frame and link to the class Invoice. In the Invoice-class is a method called hasInvoice(iNr). When I try to check if a invoice-number already exists I get a Nullpointer.
public class FinancienAanmakenFrame extends JFrame implements ActionListener {
private JLabel nummer, soort, prijs, empty, totaal;
private JTextField tfnum;
protected JTextArea tasoort, taprijs;
protected JTextField tftotaal;
private JButton ok, terug, dienst;
private JPanel p;
private Financien deFinancien;
private Voorraad deVoorraad;
private FinancienAanmakenFrame deBon;
private FinancienWijzigenFrame deWijziging;
public FinancienAanmakenFrame(Financien f, Voorraad v) {
deFinancien = f;
deVoorraad = v;
deBon = this;
p = new JPanel();
add(p);
p.setLayout(new GridLayout(6, 2, 2, 2));
nummer = new JLabel("Factuur nummer: ");
p.add(nummer);
tfnum = new JTextField();
p.add(tfnum);
dienst = new JButton("Voeg betaling toe");
p.add(dienst);
dienst.addActionListener(this);
empty = new JLabel();
p.add(empty);
soort = new JLabel("Soort dienst:");
p.add(soort);
prijs = new JLabel("Kosten:");
p.add(prijs);
tasoort = new JTextArea(20, 10);
p.add(tasoort);
tasoort.setEditable(false);
taprijs = new JTextArea(20, 10);
p.add(taprijs);
taprijs.setEditable(false);
totaal = new JLabel("Totale kosten");
p.add(totaal);
tftotaal = new JTextField("0");
p.add(tftotaal);
tftotaal.setEditable(false);
terug = new JButton("Terug naar financien menu");
p.add(terug);
terug.addActionListener(this);
ok = new JButton("Maak factuur aan");
p.add(ok);
ok.addActionListener(this);
setSize(450, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(HIDE_ON_CLOSE);
}
public boolean allesGevuld() {
if ((tfnum.getText().length() > 0) && (tftotaal.getText().length() > 0)) {
return true;
} else {
return false;
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == terug) {
this.setVisible(false);
}
else if (e.getSource() == dienst) {
DienstFrame bf = new DienstFrame(deFinancien, deVoorraad, deBon,
deWijziging);
bf.setVisible(true);
}
else if (e.getSource() == ok) {
int fN = Integer.parseInt(tfnum.getText());
if (!deFinancien.heeftFactuur(fN) && allesGevuld()) {
Factuur nwF = new Factuur(fN);
if (nwF != null) {
if (deFinancien.voegFactuurToe(nwF)) {
JOptionPane.showMessageDialog(null,
"Factuur is toegevoegd", "Succes",
JOptionPane.PLAIN_MESSAGE);
dispose();
}
}
} else {
tfnum.setText("");
JOptionPane.showMessageDialog(null,
"Er bestaat al een factuur met dit nummer", "Mislukt",
JOptionPane.PLAIN_MESSAGE);
}
} else { // niet alle gegevens zijn ingevuld
JOptionPane.showMessageDialog(null, "Vul alle gegevens in",
"Mislukt", JOptionPane.PLAIN_MESSAGE);
}
}
}
You declare deFinancien
variable like :
private Financien deFinancien;
But you never initialized the variable, and you try to access:
deFinancien.heeftFactuur(fN)
You try to initialize the same in constructor:
public FinancienAanmakenFrame(Financien f, Voorraad v) {
deFinancien = f;...
But again you are passing the same reference(instead of the value):
new DienstFrame(deFinancien, deVoorraad, deBon,
deWijziging);
That's why it is throwing null pointer exception.