Problems running Java class

Asked

Viewed 188 times

1

When I will 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
    }
}
  • 1

    Dude, try to edit your question by putting the full output of the bug.

  • Enter the line where the error occurs (not only the number because we have no way of knowing what the number is here, show the line)

  • In the Program class occurs on line 9, in the Operations class occurs on line 23;

  • As @bigown said, we have no way of knowing what the line number is, show the line.

  • @bigown and Jéferson put comments on the lines that the error occurs.

  • http://answall.com/questions/49013/exception-in-thread-main-java-lang-nullpointerexception

  • 1

    @Thank you for reminding me, and thank you for always helping me! Strong hug. Note: I am only 16 years old and when I grow up I want to be like you...

  • @Igoralisson this account is it also yours? If it is, let me know, we can merge the two if you want.

Show 3 more comments

2 answers

3


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);    
}

3

You need to instantiate the class Cliente in the variable clientes before using it. But I improve this code a little more, so:

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);
    }
}

I put in the Github for future reference.

Note that small changes greatly simplified the code.

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

Just something silly that helps understand the program. You are creating a variable that will save one client and calls her clientes. And it creates a variable that will hold multiple clients and call it cliente. It will work but this inconsistency makes right thinking more difficult. One of these variables should probably be local. The variable aux probably shouldn’t exist. The name of this class Operacoes does not seem to be an appropriate name. Otherwise maybe this class should not even exist. It seems to me that you are creating a class just by creating.

  • 1

    Why is my logic complicated? Be more specific.

  • It would have much to say beyond what I have already said. It would become a course.

  • Actually the variable 'max' does not exist I think you referred to 'aux' but it has to exist because each registered client adds up to 1 this is my intention. Thank you for the reply.

  • This, I typed wrong. There’s no reason for it to exist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.