I can not assign value to an ArrayList of an Object

1

My intention is to register the data typed in the window in the object's arraylist, but I can not!

Client class (model): Normal class, with name and address getters and setters.

Main class:

package br.projeto.view;

import java.util.ArrayList;
import br.projeto.model.Cliente;

public class Main {

    public static ArrayList <Cliente> clienteDB = new ArrayList<>();

    public static void main(String[] args) {
        //new Principal();
        //new PedidoRapido();
        new Cadastrar();
    }
}

Class Register (part that matters [window]):

btnCadastrar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Main.clienteDB.setNome("");
            }
        });

The issue is that this Main.clienteDB.setNome was only placed there to exemplify what I want to do, since I am not able to access any attribute of the Client object. What should I do?

    
asked by anonymous 28.03.2016 / 00:28

2 answers

0

It seems to me that you want to insert a new Cliente into the list. Based on this assumption:

First, create the copy of Cliente and assign the name you want:

Cliente novoCliente = new Cliente();
novoCliente.setNome("Novo cliente");
// setEndereco, setTelefone etc.

Then, enter at the bottom of the list:

Main.clienteDB.add(novoCliente);
    
28.03.2016 / 01:27
1

Main.clientDB does not return Customer, but ArrayList. To have a client and be able to do what you want you have to say which client wants Main.clienteDB.get(0).getNome() . Obviously this will return exception if your list does not have registry index 0, or index you want.

    
28.03.2016 / 01:05