Problems when executing class in Java

1

When I'm going to invoke the method of this class:

package newpackage;

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Operacoes {

    /*
     DESENVOLVIDO BY: "IIJM Team"
     */

    Cliente clientes;
    ArrayList<Cliente> cliente;


    public Operacoes() {
        cliente = new ArrayList<>();        
    }

    int aux = 0;

    public void cadastroCliente() {
        aux++;
        clientes.setCodigo(aux); // O erro ocorre quando chega aqui!
        clientes.setNome(JOptionPane.showInputDialog((aux)
                + "º Cliente\nNome: "));
        clientes.setEmail(JOptionPane.showInputDialog("E-mail: "));

        cliente.add(clientes);

    }
 }

The following problem appears:

  

Exception in thread "main" java.lang.NullPointerException

Method class main :

package newpackage;

import java.util.ArrayList;

public class Programa {

    public static void main(String[] args) {
       Operacoes op = new Operacoes();
            op.cadastroCliente(); // Ocorre um erro aqui também
    }
}
    
asked by anonymous 22.02.2015 / 19:40

2 answers

2

The variable clientes was not instantiated.

All you have to do is instantiate it ( clientes = new Cliente() ).

public void cadastroCliente() {
    clientes = new Cliente(); //adicione esta linha
    aux++;
    clientes.setCodigo(aux); // O erro ocorre quando chega aqui!
    clientes.setNome(JOptionPane.showInputDialog((aux) + "º Cliente\nNome: "));
    clientes.setEmail(JOptionPane.showInputDialog("E-mail: "));

    cliente.add(clientes);    
}
    
22.02.2015 / 19:42
3

You need to instantiate the client class in the clientes variable before using it. But I've improved this code a bit more, like this:

package newpackage;

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Operacoes {
    ArrayList<Cliente> clientes;

    public Operacoes() {
        cliente = new ArrayList<Cliente>(); //note que mudei os nomes para ficar menos confuso
    }

    public void cadastroCliente() {
        Cliente cliente = new Cliente();
        int aux = clientes.size() + 1; //pega a quantidade de elementos atual e incrementa
        cliente.setCodigo(aux);
        cliente.setNome(JOptionPane.showInputDialog((aux)
                + "º Cliente\nNome: "));
        cliente.setEmail(JOptionPane.showInputDialog("E-mail: "));

        clientes.add(cliente);
    }
}

Note that small changes have greatly simplified the code.

Your logic is very complicated, you will continue to have other problems setting up code this way. I think I had already told you this in another question but as the question is not in your account you probably created another account.

Just a silly thing that helps to understand the program. You are creating a variable that will save a client and calls it from clientes . And create a variable that will store multiple clients and call it cliente . It will work but this inconsistency makes correct thinking more difficult. One of these variables should probably be local. The aux variable should probably not exist. The name of this class Operacoes does not appear to be an appropriate name. Otherwise perhaps this class should not even exist. It seems to me that you are creating a class just by creating.

    
22.02.2015 / 20:00