1

add メソッドを実行すると、"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" が発生し続けます。

public class SortedListOfImmutables {

private Listable[] items;

/**
 * This constructor creates an empty list by creating an internal array
 * of size 0.  (Note that this is NOT the same thing as setting the internal
 * instance variable to null.) 
 */
public SortedListOfImmutables() {
    items = new Listable[0];
}

/**
 *  Copy constructor.  The current object will become a copy of the
 *  list that the parameter refers to.  
 *  
 *  The copy must be made in such a way that future changes to
 *  either of these two lists will not affect the other. In other words, 
 *  after this constructor runs, adding or removing things from one of 
 *  the lists must not have any effect on the other list.
 *  
 *  @param other the list that is to be copied
 */
public SortedListOfImmutables(SortedListOfImmutables other) {
    if(other != null) {
        items = new Listable[other.items.length];

        for(int index=0;index<other.items.length;index++){
            items[index] = other.items[index];
        }
    }
}

/**
 * Adds an item to the list.  This method assumes that the list is already
 * sorted in alphabetical order based on the names of the items in the list.
 * 
 * The new item will be inserted into the list in the appropriate place so
 * that the list will remain alphabetized by names.
 * 
 * In order to accommodate the new item, the internal array must be re-sized 
 * so that it is one unit larger than it was before the call to this method.
 *  
 * @param itemToAdd refers to a Listable item to be added to this list
 */
public void add(Listable itemToAdd) {
    if(itemToAdd != null) {
        SortedListOfImmutables temp = new SortedListOfImmutables();
        temp.items = new Listable[this.items.length+1];
        boolean added = false;

        if(this.items.length == 0)
            temp.items[0] = itemToAdd;

        else {
            for(int index = 0;index < this.items.length;index++) {
                if(this.items[index].getName().compareTo(itemToAdd.getName())>0)
                    temp.items[index] = items[index];
                else {
                    if(!added) {
                        temp.items[index] = itemToAdd;
                        added = true;
                    }
                    else temp.items[index+1] = this.items[index];
                }
            }
        }
        this.items = new Listable[temp.items.length];

        for(int tempIndex = 0;tempIndex < temp.items.length;tempIndex++) {
            this.items[tempIndex] = temp.items[tempIndex];
        }
    }
}

Listable はクラス Menagerie および Animal によって実装されるインターフェースであり、Menagerie オブジェクトは動物のグループです。どうやら問題は、items 配列を割り当てたが、その要素を割り当てていないことです。そのため、this.items[tempIndex] を新しいオブジェクトとして初期化する必要があります。ただし、それは Animal または Menagerie のいずれかである可能性があり、instanceOf() を使用してどちらであるかを調べることは許可されていません。また、アニマル コンストラクタはプライベートであるため、最初から新しいアニマル オブジェクトを作成することはできません。動物を項目配列に追加しようとすると、最初の動物は問題なく追加されますが、さらに追加しようとすると nullpointerexception が発生します。解決策は単純なものであると確信しており、これを考えすぎているだけです。

4

3 に答える 3

1

null引数を指定してコピーコンストラクターを呼び出すと、が呼び出され、でNPEが取得されitemsます。argがnullのときにコピーコンストラクタから例外をスローするか、すべての場合に初期化します。nulladditems

編集あなたのコードは非常に非効率的です。ArrayListプライベートitemsフィールドにはを使用することをお勧めします。それはあなたのために動的なサイジングのすべての仕事をします。このようなものが機能するはずです:

public class SortedListOfImmutables {

    private List<Listable> items;

    /**
     * This constructor creates an empty list.
     */
    public SortedListOfImmutables() {
        items = new ArrayList<Listable>();
    }

    /**
     *  Copy constructor.  The current object will become a copy of the
     *  list that the parameter refers to.  
     *  
     *  The copy must be made in such a way that future changes to
     *  either of these two lists will not affect the other. In other words, 
     *  after this constructor runs, adding or removing things from one of 
     *  the lists must not have any effect on the other list.
     *  
     *  @param other the list that is to be copied
     */
    public SortedListOfImmutables(SortedListOfImmutables other) {
        items = (other == null)
            ? new ArrayList<Listable>()
            : new ArrayList<Listable>(other.items);
    }

    /**
     * Adds an item to the list. This method assumes that the list is already
     * sorted in alphabetical order based on the names of the items
     * in the list.
     * 
     * The new item will be inserted into the list in the appropriate place so
     * that the list will remain alphabetized by names.
     *
     * itemToAdd will not be added if it is null or if it equals an item
     * already in the list.
     *  
     * @param itemToAdd refers to a Listable item to be added to this list
     */
    public void add(Listable itemToAdd) {
        if (itemToAdd != null) {
            for (int i = 0; i < items.size(); ++i) {
                Listable currentItem = items.get(i);
                int comp = itemToAdd.compareTo(currentItem);
                if (comp > 0) continue;
                if (comp < 0) {
                    // do this unconditionally if you want to allow duplicates
                    items.add(i, itemToAdd);
                }
                return;
            }
        }
    }
}
于 2012-12-02T21:01:41.913 に答える
1

どうやら、問題は、アイテム配列を割り当てたが、その要素を割り当てたことがないことです

うん。これは間違いなくヌルポインタ例外を引き起こす可能性のある問題です;)

ただし、それはアニマルまたはメナジェリーのいずれかである可能性があります。

Q: "Animal" と "Menagerie" はどちらも共通の親クラスの子ですか?

于 2012-12-02T21:05:36.963 に答える
0

コードを次のように変更します

public SortedListOfImmutables(SortedListOfImmutables other)
    if(other != null) {
        items = new Listable[other.items.length];

        for(int index=0;index<other.items.length;index++){
            items[index] = other.items[index];
        }
    } else {
       items = new Listable[0];
    }
}

そうしないと、新しいオブジェクトにnullリストを作成できます。

于 2012-12-02T21:06:31.880 に答える