Java 使用关联时使用空指针

Java 使用关联时使用空指针,java,jframe,joptionpane,Java,Jframe,Joptionpane,我正在用Java制作一个发票程序。我正在制作一个框架并链接到类发票。Invoice类中有一个名为hasInvoice(iNr)的方法。当我试图检查发票号是否已经存在时,我得到一个空指针 public class FinancienAanmakenFrame extends JFrame implements ActionListener { private JLabel nummer, soort, prijs, empty, totaal; private JTextFiel

我正在用Java制作一个发票程序。我正在制作一个框架并链接到类发票。Invoice类中有一个名为hasInvoice(iNr)的方法。当我试图检查发票号是否已经存在时,我得到一个空指针

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);
        }
    }
}

声明
definanceen
变量如下:

private Financien deFinancien;
但您从未初始化变量,并且尝试访问:

deFinancien.heeftFactuur(fN)
您尝试在构造函数中初始化相同的值:

public FinancienAanmakenFrame(Financien f, Voorraad v) {
        deFinancien = f;...
但同样,您正在传递相同的引用(而不是值):


这就是它抛出空指针异常的原因。

请发布stacktraceis
deFinancien
已初始化?是的。public financian makenframe(financian f,Voorraad v){deFinancien=f;devorraad=v;deBon=this;@user3504320问题解决了吗?您必须传递deFinancien的对象值而不是引用。例如:Financien deFinancien 1=new Financien();//这将创建对象。deFinancien1.setSomething()//填充某些内容。然后在新的DienstFrame(定义1…)中传递值
new DienstFrame(deFinancien, deVoorraad, deBon,
                    deWijziging);