I'd like to understand what's the error here. I'm trying to declare a String variable which contains the name attribute of another class like so:
public static void findContact() {
int se = 1;
String nom;
String nomS;
Contact ce;
while (se == 1) {
nom = JOptionPane.showInputDialog("Name of desired contact: ");
int i;
for (i = 0; i <= it; i++) {
ce = c[i];
nomS = ce.getName();//here is the error
if (nomS.equalsIgnoreCase(nom)) {
System.out.println(c[i]);
}
}
se = Integer.parseInt(JOptionPane.showInputDialog("Would you like to search for another contact [1]Y [2]N"));
}
}
c, the contacts list was declared as a constant like so[i'll omit unnecessary code]:
private static final Contact c[] = new Contact[100];
and the contacts class was declared like so [i'll show the relevant parts]:
public class Contact {
private String name;
private int phone;
private String mail;
public Contact() {
name = " ";
phone = 0;
mail = " ";
}
public Contacto(String name, int phone, String mail) {
this.name = name;
this.phone = phone;
this.mail = mail;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + phone + mail;
}
I've tried creating another Contact type variable inside the method:
Contact ce = new Contact();
or
Contact ce = new Contact("",1,"");
To no avail. The worst part is that the code actually compiles and shows what it's supposed to show, it does compare and show the contact as if it worked perfectly but i still get this [My variables are in spanish in the actual code]: Java console result
Thanks in advance to any and all reading and helping.
Edit: I have an array filler method:
public static void addContact() {
String nom;
int phone;
String mail;
int se = 1;
while (se == 1 ) {
nom = JOptionPane.showInputDialog("Name of contact: ");
phone = Integer.parseInt(JOptionPane.showInputDialog("Phone number: "));
mail = JOptionPane.showInputDialog("contact's mail");
System.out.println(nom + " " + phone + " " + mail);
Contact con = new Contact(nom, phone, mail);
c[it] = con;
System.out.println(c[0]);
it++;
se = Integer.parseInt(JOptionPane.showInputDialog("Add another contact 1Y 2N"));
}
}
So the array is filled with at least 1 at any given moment.