-2

my error is:

Exception in thread "main" java.lang.NullPointerException
    at ClienteDB.incluir(ClienteDB.java:30)
    at ClienteInterface.main(ClienteInterface.java:16)

I just checked some other questions, but didn't found why this is happening. Usually people say that have some variable with value null. In a first moment, the variable will be null, but i didn't do the assignment with null.

Line 30 is: c[npp].setCpf(ccpf); That is one method of my program, but the only one that is not working.

Thanks in advance for the help!

import javax.swing.JOptionPane;

public class ClienteDB
{
Cliente c[]= new Cliente[11];

public void incluir() {
    int flag = 0;
    String np = JOptionPane
            .showInputDialog("Entre a posição que você deseja incluir um usuário (1 a 10):");
    int npp = Integer.parseInt(np);
    if (c[npp] != null) {
        JOptionPane.showMessageDialog(null,
                "Já possui um cliente neste campo.");
    } else {
        String ccpf = JOptionPane.showInputDialog("Qual o cpf do cliente "
                + npp + "?");
        for (int np2 = 1; np2 < 11; np2++) {
            if ((c[np2] != null) && (c[np2].getCpf().equals(ccpf))) {
                JOptionPane.showMessageDialog(null,
                        "Existe um usuário com este CPF.\n");
                flag = 1;
                break;
            } else {
                c[npp].setCpf(ccpf);
                break;
            }
        }
        if (flag != 1) {
            String cnome = JOptionPane
                    .showInputDialog("Qual o nome do cliente " + npp + "?");
            c[npp].setNome(cnome);
            String cend = JOptionPane
                    .showInputDialog("Qual o endereço do cliente " + npp
                            + "?");
            c[npp].setEndereco(cend);
            String ctel = JOptionPane
                    .showInputDialog("Qual o telefone do cliente " + npp
                            + "?");
            c[npp].setTelefone(ctel);
        }
    }
}

Thats my main:

   import javax.swing.JOptionPane;

public class ClienteInterface
{
public static void main (String args[])
{
  ClienteDB cc = new ClienteDB();
    int funcao;
    do{
        String fc=JOptionPane.showInputDialog("Bem vindo!\nQual função você deseja?\n1-Incluir Cliente\n2-Consultar Cliente\n3-Alterar Cliente\n4-Excluir Cliente\n5-Listar clientes\n9-Fim");
        funcao=Integer.parseInt(fc);

     switch (funcao)
     {
        case 1:
        cc.incluir();
        break;

        case 2:
        cc.consultar();
        break;

        case 3:
        cc.alterar();
        break;

        case 4:
        cc.excluir();
        break;

        case 5:
        cc.listar();
        break;

        case 9:
        break;
     }                  
  }while (funcao!=9);
}

}

2 Answers2

1

Looks like you forgot to initialize your array. Did you do

c = new (data_type)[length] 
Kick
  • 4,823
  • 3
  • 22
  • 29
Steve
  • 11,696
  • 7
  • 43
  • 81
1

Now that we know which line it is, the problem lies in this if

    if (c[npp] != null) { // HERE YOU CHECK IT IS NOT NULL
    JOptionPane.showMessageDialog(null,
            "Já possui um cliente neste campo.");
} else {
// PASS THIS POINT, c[npp] CAN ONLY BE NULL 
    String ccpf = JOptionPane.showInputDialog("Qual o cpf do cliente "
            + npp + "?");
    for (int np2 = 1; np2 < 11; np2++) {
        if ((c[np2] != null) && (c[np2].getCpf().equals(ccpf))) {
            JOptionPane.showMessageDialog(null,
                    "Existe um usuário com este CPF.\n");
            flag = 1;
            break;
        } else {
            c[npp].setCpf(ccpf); // NULL POINTER... FOR A GOOD REASON
            break;
        }
    }

You either need to change the if for

if (c[npp] == null) {

or you wanted to write

c[np2].setCpf(ccpf);

instead.

I know Eclipse IDE can find this on the fly if set properly. If you aren't using a IDE, I strongly suggest you start to.

Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
  • i didn't use IDE. i'm using jGRASP for a university grade now. but your answer do not solve my problem :/ i will try IDE probably. many thanks for the help! – user3353265 Feb 26 '14 at 19:52