Array problem in Java

-1

I'm trying to create a contacts calendar in java, I'm using some classes that were requested in the exercise.

the main class:

    import java.util.Scanner;

    public class Lista15Q03 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int max = 3;

        Endereco end[] = new Endereco[max];

        Telefones tel[] = new Telefones[max];

        Emails email[] = new Emails[max];

        Contatos con[] = new Contatos[max];
        Contatos c = new Contatos();
        c.setEndereco(end);

        int op;
        int indice = 0;

        do {

            System.out.println("1 - adicionar");
            System.out.println("2 - listar");
            System.out.println("3 - mostrar");
            System.out.println("4 - alterar");
            System.out.println("5 - apagar");
            System.out.println("6 - sair");
            System.out.print("escolha: ");
            op = scan.nextInt();

            switch(op){
                case 1:
                    if(indice < max){

                        c = new Contatos();

                        System.out.println("nome: ");
                        String nome = scan.next();
                        c.setNome(nome);

                        System.out.print("nome da rua: ");
                        String rua = scan.next();
                        end[indice].setRua(rua);

                        System.out.print("número: ");
                        String num = scan.next();
                        end[indice].setNumero(num);

                        System.out.print("bairro: ");
                        String bairro = scan.next();
                        end[indice].setRua(bairro);

                        con[indice] = c;
                        indice++;
                    }else{
                        System.out.println("");
                        System.out.println("lista de contatos cheia");
                        System.out.println("");
                    }
                    break;
                case 2:
                    break;
                case 6:
                    break;
                default:
                    System.out.println("opção inválida");
            }

        } while (op != 6);

    }

}

the class of contacts:

public class Contatos {
    private String nome;
    private Endereco[] endereco;
    private Emails[] email;
    private Telefones[] telefones;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Endereco[] getEndereco(){
        return endereco;
    }

    public void setEndereco(Endereco[] endereco){
        this.endereco = endereco;
    }

    public Emails[] getEmail() {
        return email;
    }

    public void setEmail(Emails[] email) {
        this.email = email;
    }

    public Telefones[] getTelefones() {
        return telefones;
    }

    public void setTelefones(Telefones[] telefones) {
        this.telefones = telefones;
    }

}

error running on terminal:

  

Exception in thread "main" java.lang.NullPointerException           at List15Q03.main (List15Q03.java:46)

error running on netbeans:

Exception in thread "main" java.lang.NullPointerException
    at lista15q03.Lista15Q03.main(Lista15Q03.java:41)
/home/henrique/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 5 segundos)
    
asked by anonymous 03.01.2017 / 16:59

3 answers

1

You have failed to instantiate the objects in the positions of the arrays end[] , tel[] , email .

In calls

Endereco end[] = new Endereco[max];

Telefones tel[] = new Telefones[max];

Emails email[] = new Emails[max];

Only the array of each of these objects with new operator were instantiated.

For each array you need a for that runs through the array by instantiating the objects

As in the example below

  for (int i = 0; i < max; i++) {
            end[i] = new Endereco();
        }

However, I notice that the Endereco , Emails and Phones objects are already attributes of the Contacts . Thus, a more correct way would be to remove them from the main class and add a method for initializing these objects into their own classes. As in the example below.

public class Contatos {

    int max;
    private String nome;
    private Endereco[] endereco;
    private Emails[] email;
    private Telefones[] telefones;

    public Contatos(int max) {
        this.max = max;
        initEnderecos();
   }

    public String getNome() {
        return nome;
   }

    public void setNome(String nome) {
        this.nome = nome;
   }

    public Endereco[] getEndereco() {
       return endereco;
   }

    public void setEndereco(Endereco[] endereco) {
        this.endereco = endereco;
  }

    public Emails[] getEmail() {
        return email;
  }

    public void setEmail(Emails[] email) {
       this.email = email;
  } 

    public Telefones[] getTelefones() {
       return telefones;
  }

   public void setTelefones(Telefones[] telefones) {
      this.telefones = telefones;
  }

   private void initEnderecos() {
        for (int i = 0; i < max; i++) {
            this.endereco[i] = new Endereco();
        }
   }
} 

In this example I moved the max attribute, which is the maximum size of the contact list for the Contato class, and initialize only the array of Endereco . The others you can do in an analogous way.

Note that this is just one of the improvements you can make to this code

    
03.01.2017 / 17:36
1

Hello, the problem that is happening is that I just declare the value for array as new Address, however every item of it gets null until it is initialized, so you have to do this initialization before joining values to the variables of that object. p>

 switch(op){
             case 1:
                 if(indice < max){

                     end[indice] = new Endereco();
                     c = new Contato();

                     System.out.println("nome: ");
                     String nome = scan.next();
                     c.setNome(nome);

                     System.out.print("nome da rua: ");
                     String rua = scan.next();
                     end[indice].setRua(rua);

                     System.out.print("número: ");
                     String num = scan.next();
                     end[indice].setNumero(num);

                     System.out.print("bairro: ");
                     String bairro = scan.next();
                     end[indice].setBairro(bairro);

                     c.setEndereco(end);
                     con[indice] = c;
                     indice++;
                 }

Another thing I noticed by analyzing your code is that it directs an address in the object c, however dps initializes it again, losing what was set that in case it had nothing, the most correct thing to do is just put this value after inserting all the items of the address, and tbm was putting the value of the neighborhood in the street.

    
03.01.2017 / 17:44
1

If you can use List , you will be happier, if you can not, you will have to create an object for each Array you created, because each Array position is waiting for an object and you are passing a String. so in your case it would look like this.

Obs: I'll just do one of the Array and you'll replicate.

Endereco []end = new Endereco[max]; // cada posição tem que ser um objeto
Endereco endereco = new Endereco();

System.out.print("nome da rua: ");
String rua = scan.next();
endereco.setRua(rua);
end[indice] = endereco;

end[indice].getRua(); //para mostrar o valor depois.
    
08.01.2017 / 23:05