-1
I am trying to create a calendar of contacts in java, I am 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 lista15q03.main(Lista15q03.java:46)
error running in 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)
Where is line 46?
– user28595
Put the full stack trace.
– Wilker
It could further detail the project and where the problem occurs?
– André Araujo
line 46 is this: end[Indice]. setRua(street);
– Henrique Nascimento
The two errors occur in different lines, one says line 41 and another 43
– user28595
I have to create an agenda with crud functionality, so I created 4 classes, Email, Phones, Address and Contacts. I refer to other classes in the contacts class as array. In the main class of the program, I instate the Contacts class and also play it within the array.
– Henrique Nascimento
That’s why in netbeans you have the package line and in sublime you don’t. So the error is really from this snippet: end[Indice]. setRua(street);
– Henrique Nascimento
This vector input was not initialized, you just initialized it as an array, but not individually
– André Araujo
You have created an address array but the vector has no address type object. So you are accessing an empty array. This is why nullpointer occurs.
– user28595
then in case I have to instantiate Address and throw the object in c.setEndereco(); ?
– Henrique Nascimento