Exception in thread & ldquo; Hand & rdquo; Java.lang.NullPointerException

advertisements

This question already has an answer here:

  • What is a NullPointerException, and how do I fix it? 12 answers

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

}


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

c = new (data_type)[length]