简体   繁体   English

将项目添加到不同类的ArrayList中

[英]Adding item to ArrayList in different class

For an assignment I have to make a Java application and one of the functions I'm having a problem with is adding to an ArrayList . 对于一项任务,我必须制作一个Java应用程序,而我遇到问题的功能之一是添加到ArrayList The ArrayList is in a different class then the method and I get a nullpointer (doesn't happen when I make it static). ArrayList与方法位于不同的类中,并且我得到了一个空指针(将其设为静态时不会发生)。

This is the code where I'm trying to add: 这是我要添加的代码:

if (e.getSource() == voegToe) {
   if (naam.equals("") || straat.equals("") || huisnummer.equals("")
                || postcode.equals("") || plaats.equals("")
                || telefoonnummer.equals("") || email.equals("")) {

            allesoke = false;
            foutmelding += "\n";
  }

  if (allesoke == true) {
      Klant newKlant = new Klant (naam, postcode, huisnummer);
      System.out.println("HOI");
      klantenBindingRef.alleKlanten.add(newKlant);
      System.out.println(klantenBindingRef.alleKlanten);
  } else {
      JOptionPane.showMessageDialog(null, foutmelding, "Foutmelding",
            JOptionPane.INFORMATION_MESSAGE, null);
  }

And this is part of the code of the class with the ArrayList : 这是ArrayList类的代码的一部分:

public class Klantenbinding {
private String naam;
protected List<Klant> alleKlanten = new ArrayList<Klant> ();

// klanten
public void voegKlantToe(Klant nweKlant) {

    if (!heeftKlant(nweKlant.getNaam())) {
        alleKlanten.add(nweKlant);
    }
}
    public boolean heeftKlant(String naam) {
    boolean b = false;
    for (Klant k : alleKlanten) {
        if (k.getNaam() == naam) {
            b = true;
        }
    }
    return b;
}

The nullpointer comes from the line: 空指针来自以下行:

klantenBindingRef.alleKlanten.add(newKlant);

public class KlantToevoegen extends JFrame implements ActionListener {
/**
 * 
 */
JFrame frame;
private static final long serialVersionUID = 1L;
private JButton voegToe, terug;
private JPanel contentPane;
private JTextField nmTf, strTf, hnrTf, pcTf, plTf, telnrTf, emailTf;
private JLabel lblAanhef, lblNaam, lblStraat, lblNieuweKlantToevoegen,
        lblHuisnr, lblPostcode, lblPlaats, lblTelnr, lblEmail;
private JComboBox aanhefBox;
private Klantenbinding klantenBindingRef;
private AutoToevoegenFrame autoToevoegenFrameRef;
private JSeparator separator;
private JSeparator separator_1;

/**
 * Launch the application.
 */

Did you try importing the class with the ArrayList in it? 您是否尝试导入其中包含ArrayList的类? (can't comment yet) (尚无法评论)

我敢打赌,您的变量klantenBindingRef永远不会初始化。

Try comparing your strings in this way to avoid NPE 尝试以这种方式比较字符串以避免NPE

if ("".equals(naam) || "".equals(straat) || ...) {
    allesoke = false;
    foutmelding += "\n";
}

then verify that klantenBindingRef is instantiated before access to his variables 然后验证klantenBindingRef已实例化,然后再访问其变量

//klantenBindingRef is null
klantenBindingRef = new Klantenbinding();
klantenBindingRef.alleKlanten
...

initialize your varibale klantenBindingRef before use klantenBindingRef = new Klantenbinding(); 在使用klantenBindingRef = new Klantenbinding();之前初始化您的变量klantenBindingRef klantenBindingRef = new Klantenbinding();

if( klantenBindingRef == null ) 
    klantenBindingRef = new Klantenbinding();
klantenBindingRef.alleKlanten.add(newKlant);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM