problem getting objects stored in an arrayList in a repository

0

I'm doing a Java desktop app with the M.V.P architecture for now I'm simulating the database in an arrayList in a repository

public class RepositorioClientes {

private static ArrayList<Cliente> listaClientes;

public RepositorioClientes() {
    this.listaClientes = new ArrayList<>();
}

public void setListaClientes(Cliente cliente) {
    this.listaClientes.add(cliente);
}

public ArrayList<Cliente> getListaClientes() {
    return this.listaClientes   
}  
}

the issue is that when I want to get that arrayList it returns it null.

That's where I call the repository to get the array list:

public class PresentadorTransferencias {

private VistaTransferencias vistaTransferencias;

private RepositorioClientes repositorioClientes;

public PresentadorTransferencias(VistaTransferencias vistaTransferencias) {
    this.vistaTransferencias = vistaTransferencias;
    this.repositorioClientes = new RepositorioClientes();
}
    public void inicializarCombobox() {

    for (Cliente cliente : repositorioClientes.getListaClientes()) {

        vistaTransferencias.getDesdejComboBoxTransferencia().addItem(cliente.toString());

        vistaTransferencias.getHaciajComboBoxTransferencia().addItem(cliente.toString());

    }
   }
  }

My question is, how do I solve this problem in order to obtain the previously saved objects?

    
asked by nicolas gigena 04.09.2018 в 16:39
source

1 answer

1

Returns the empty list because you are emptying it in the constructor of the class:

public RepositorioClientes() {
    this.listaClientes = new ArrayList<>();
}

And you call it when you initialize a repository variable:

this.repositorioClientes = new RepositorioClientes();

What you must do is initialize the list when declaring the static variable:

private static ArrayList<Cliente> listaClientes = new ArrayList<>();
    
answered by 04.09.2018 / 18:00
source