0

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.

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Flame
  • 1
  • 1
  • 1
    Read the duplicate *carefully* as it will explain the problem and the general steps that you must take to debug NullPointerExceptions (NPE). – Hovercraft Full Of Eels Oct 12 '19 at 21:40
  • 1
    Your `c` array (a terrible and vague variable name by the way) contains null values -- do you fill the array with objects before using it? – Hovercraft Full Of Eels Oct 12 '19 at 21:40
  • 1
    When you create an array of reference type (here Contact -- or Contacto? -- type), the array is initially filled with nothing but null values. You cannot use any of the array items until you first create new Contact objects and put them into the array. Think of a reference array like an empty egg carton. You can't make an omelet with the contents of the carton until it is first filled with eggs. – Hovercraft Full Of Eels Oct 12 '19 at 21:42
  • I do fill the array before using it, with the inputs shown on the image of the console results. I'm reading the duplicate but i've tried Contact ce = new Contact();already – Flame Oct 12 '19 at 21:53
  • Flame, the JVM is proving to you that not all items in the array have valid objects assigned, and at least one that you try to use most definitely is null. Use your debugger to see why, since I'm afraid that even if we wanted to help, we can't since only you have the working code. – Hovercraft Full Of Eels Oct 12 '19 at 21:53
  • Thanks Dude. I found it. on the for (i = 0; i <= it; i++) loop, since it was <= instead of < it would run it till the last, unfilled position of the array, throwing that exception. – Flame Oct 12 '19 at 22:12
  • 1
    Yay, debugging for the win! – Hovercraft Full Of Eels Oct 12 '19 at 22:15

0 Answers0