user4799279
user4799279

Reputation:

Enhanced for-loop stopped with NullPointerException

My enhanced for loop doesn't seem to be iterating correctly. The purpose is to use the search class to go through an ArrayList of type Contact and find a specific name but for some reason it only goes through the first contact and stops with an error after that displaying:

Exception in thread "main" java.lang.NullPointerException
    at client.AddressBook.search(AddressBook.java:17)
    at Main.main(Main.java:31)

My Main class is below:

import client.AddressBook;
import client.Contact;

public class Main {

    public static void main(String[] args) {

        AddressBook ab = new AddressBook();

        Contact c1 = new Contact("[email protected]");
        ab.add(c1);

        Contact c2 = new Contact("[email protected]", "Jeff Meunier", "jeff");
        ab.add(c2);

        Contact c3 = new Contact("[email protected]", "Bill Gates", "bill");
        ab.add(c3);

        System.out.println(ab.search("jeff"));
    }

}

The AddressBook and Contact class are also listed below:

package client;
import java.util.ArrayList;

public class AddressBook {

    ArrayList<Contact> al = new ArrayList<Contact>();

    public void add(Contact contactAdd) {

        al.add(contactAdd);
    }

    public Contact search(String searchName) {

        for(Contact obj: al) {

            if(obj.getNickName().equals(searchName)) {
                return obj;
            }
        }
        return null;
    }

    public String remove(String nickname) {

        search(nickname);
        al.remove(nickname);

        return nickname;
    }

    public void show() {

        int x = 1;
        for(Contact obj: al) {

            System.out.println(x + ". " + obj.toString());
            x++;
        }
    }
}



package client;

public class Contact {

    public String _emailAddress = null;
    public String _fullName = null;
    public String _nickName = null;

    public Contact(String emailaddress, String fullname, String nickname) {

        _emailAddress = emailaddress;
        _fullName = fullname;
        _nickName = nickname;
    }

    public Contact(String emailaddress) {

        _emailAddress = emailaddress;
    }

    @Override
    public String toString() {

        if(_fullName == null & _nickName == null) {

            //System.out.println("<" + _emailAddress + ">");
            return _emailAddress;
        }
        else {
            //System.out.println(_fullName + " (" + _nickName + ") " + "<" + _emailAddress + ">");
            return _fullName + " (" +  _nickName + ") " + "<" + _emailAddress + ">";
        }
    }

    public String getNickName() {

        return _nickName;
    }
}

If anyone can give any pointers it would be greatly appreciated. Ultimately right now I am only testing to see whether the search class can search for a specified nickname and then print out the returned value of that. Obviously it should be returning the second Contact (or at least that is the intention).

Upvotes: 2

Views: 1003

Answers (2)

LhasaDad
LhasaDad

Reputation: 2153

Your first item you add does not include a nickname. in your search you get the nickname and call equals() on a null reference.

Upvotes: 1

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

The problem happen in this validation :

 if(obj.getNickName().equals(searchName)) {
     return obj;
 }

It seems like obj.getNickName() may sometime be null.

Change the order of your validation :

public Contact search(String searchName) {

    for(Contact obj: al) {
        //I assume that searchName will never be null
        if(searchName.equals(obj.getNickName()) {
            return obj;
        }
    }
    return null;
}

Upvotes: 2

Related Questions