Equals方法抛出NullPointerException

时间:2014-04-13 12:19:04

标签: java arrays nullpointerexception

美好的一天,

我创建了一个对象动态数组和一个delete方法,用于删除该数组中的特定对象。我创建了另一个私有方法,用于检测具有相同姓氏和首字母的2个对象。 这是我的代码。

public Entry[] entryDirectory = new Entry[0];

@Override
public void addEntry(Entry newEntry) {
    int index = findFreeSpace();

    if (index >= 0) {
        entryDirectory[index] = newEntry;
    }
    else {
    entryDirectory = Arrays.copyOf(entryDirectory,
            entryDirectory.length + 1);
    entryDirectory[entryDirectory.length - 1] = newEntry;
    }
}

private int findFreeSpace() {

    for (int i = 0; i < entryDirectory.length; i++) {
        if (entryDirectory[i] == null) {
            return i;
        }
    }

    return -1;
}

private Entry[] findEntries(String name) {

    Entry[] returnEntries = new Entry[0];

    for (int i = 0; i < entryDirectory.length; i++) {
        if ((entryDirectory[i].getSurname() + " " + entryDirectory[i]
                .getInitials()).equalsIgnoreCase(name)) {

            returnEntries = Arrays.copyOf(returnEntries,
                    returnEntries.length + 1);
            returnEntries[returnEntries.length - 1] = entryDirectory[i];
        }
    }
    return returnEntries;
}

@Override
public void deleteEntry(String name) {

    if (findEntries(name).length == 1) {

        for (int i = 0; i < entryDirectory.length; i++) {

            if (entryDirectory[i].equals(findEntries(name)[0])) {
                entryDirectory[i] = null;
            }
        }
    }

我已使用以下代码对此进行测试:

    d.addEntry(new Entry("a", "a", 1234));
    d.addEntry(new Entry("a", "b", 12345));
    d.addEntry(new Entry("a", "c", 123456));
    d.addEntry(new Entry("a", "d", 123456));
    d.addEntry(new Entry("a", "e", 123456));

    d.deleteEntry("a a");

使用deleteEntry方法时抛出nullpointer异常。但是,如果我将if语句中的布尔逻辑从deleteEntry方法更改为

(entryDirectory[i].getSurname() + " " + entryDirectory[i]
                    .getInitials()).equalsIgnoreCase(name)

deleteEntry方法可行。为什么.equals()方法在我的情况下显示为false?

干杯

2 个答案:

答案 0 :(得分:0)

您是否覆盖了Entry类中的equals方法?

如果不是肯定会返回false。

所以重写equals方法并在equals中添加等式逻辑,如下所示

public boolean equals(Entry o){

boolean result;
    if((o== null) || (getClass() != o.getClass())){
        result = false;
    } // end if
    else{
        Entry e= (Entry)o;
        result = surname.equals(o.surname) &&  initials.equals(o.initials);
    } // end else

    return result;

}

答案 1 :(得分:0)

终于搞定了!但是你应该真正改变代码的逻辑

private Entry[] findEntries(String name) {

    Entry[] returnEntries = new Entry[0];

    for (int i = 0; i < entryDirectory.length; i++) {

        if (entryDirectory[i] != null) {

            if ((entryDirectory[i].getSurname() + " " + entryDirectory[i]
                    .getInitials()).equalsIgnoreCase(name)) {

                returnEntries = Arrays.copyOf(returnEntries,
                        returnEntries.length + 1);
                returnEntries[returnEntries.length - 1] = entryDirectory[i];
            }
        }
    }
    return returnEntries;
}

请注意,您在以下行中输入了entryDirectory null:

if (entryDirectory[i].equals(findEntries(name)[0])) {
    entryDirectory[i] = null;// this is the source of exception
}

我甚至更进了一步,因为在解决了这个问题后你会得到另一个例外。 有趣的是,因为findEntries内的条件可能不满足,所以它可能会返回一个空数组,所以你应该把它改成这个:

for (int i = 0; i < entryDirectory.length; i++) {
    if (findEntries(name).length != 0) {
        if (entryDirectory[i].equals(findEntries(name)[0])) {
            entryDirectory[i] = null;// this is the source of exception
        }
    }
}