0

I have a problem with this part of the code. It seems that if I try to add something to a list of Client I get a NullPointerException. I really don't know why because if I debug this part of the code the variable of type Client has information. If someone can help me I would be grateful. Here is the part of the code where the exception appears:

public class Customers {
    private ArrayList<Client> listaClienti;
    public Customers()
    {
    }
    public void addClient(Client c,int i)
    {
        listaClienti.add(i, c);
    }
    public void deleteClient(Client c)
    {
        listaClienti.remove(c);
    }
    public Client getClient(int id)
    {
        return listaClienti.get(id);
    }
}
Idos
  • 14,036
  • 13
  • 48
  • 65
Ioana
  • 67
  • 2
  • 5

4 Answers4

4

You are not instantiating your list:

private ArrayList<Client> listaClienti = new ArrayList<>();

You may also instantiate inside your class constructor if you wish:

public Customers() {
    listaClienti = new ArrayList<>();
}
Idos
  • 14,036
  • 13
  • 48
  • 65
0

You got a NullPointerException because you didn't initialize your list

private List<Client> listaClienti = new ArrayList<>();
karim mohsen
  • 2,051
  • 1
  • 11
  • 18
0

first you have to insatantiate your array

ArrayList<Client> listaClienti = new ArrayList<>();
Parsania Hardik
  • 4,325
  • 1
  • 29
  • 33
0

or

 public Customers()
  {
    listaClienti = new ArrayList<Client>();
 }
wayne
  • 1
  • 1