简体   繁体   中英

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("jeffm@engr.uconn.edu");
        ab.add(c1);

        Contact c2 = new Contact("jeffm@engr.uconn.edu", "Jeff Meunier", "jeff");
        ab.add(c2);

        Contact c3 = new Contact("billgates@engr.uconn.edu", "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).

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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM