0

文字列の配列を初期化して「サイズ」要素と使用済み要素の量を 0 にするオブジェクトを作成する割り当てがあります。

私の問題は、文字列を比較してアルファベット順に並べようとするときです。

int compare = storage[index].compareTo(value);
if (compare < 0)

nullpointerexception の実行時エラーが発生する場所

これが私の完全なコードです。

クラスメイン

package assignment2;
public class Main {
    public static void main(String[] args) {
        OrderedStringList myList = new OrderedStringList(5);

        System.out.println("adding 10, 5, & 7");
        myList.Insert("10");
        myList.Insert("5");
        myList.Insert("7");

        myList.Display();

        System.out.println("Value 4 find = "+ myList.Find("4"));
        System.out.println("Value 7 find = "+ myList.Find("7"));

        System.out.println("Adding 24 & 3");
        myList.Insert("24");
        myList.Insert("3");

        myList.Display();

        System.out.println("myList size: "+ myList.Size());

        if (!myList.Insert("12"))
            System.out.println("Could not add 12, full");

        System.out.println("Removing 10, adding 12.");
        myList.Delete("10");
        myList.Insert("12");

        myList.Display();
    }
}

クラス OrderedStringList

package assignment2;
public class OrderedStringList {
    int length;
    int numUsed;
    String[] storage;
    boolean ordered;

    public OrderedStringList(int size){
        length = size;
        storage = new String[length];
        numUsed = 0;
    }


    public boolean Insert(String value){
        boolean result = false;
                int index = 0;
                if (numUsed < length) {
                    while (index <= numUsed) {
                        int compare = storage[index].compareTo(value);
                        if (compare < 0)
                            index++;
                    }
                    moveItemsDown(index);
                    storage[index] = value;
                    numUsed++;
                    result = true;
                }
                return result;
    }
    private void moveItemsDown(int start){
        int index;
        for (index = numUsed-1; index >=start; index--){
            storage[index+1] = storage[index];
        }
    }

    private void moveItemsUp(int start){
        int index;
        for (index = start; index < numUsed-1; index++){
            storage[index] = storage[index+1];
        }
    }

    public boolean Find(String value){
        return (FindIndex(value) >= 0);
    }

    private int FindIndex(String value) {
        int result = -1;
        int index = 0;
        boolean found = false;
        while ((index < numUsed) && (!found)) {
            found = (value.equals(storage[index]));
            if (!found)
                index++;
        }
        if (found)
            result = index;
        return result;
    }

    public boolean Delete(String value){
        boolean result = false;
        int location;
        location = FindIndex(value);
        if (location >= 0) {
            moveItemsUp(location);
            numUsed--;
            result = true;
        }
        return result;
    }

    public void Display() {
        int index;
        System.out.println("list Contents: ");
        for (index = 0; index < numUsed; index++) {
            System.out.println(index+" "+storage[index]);
        }
        System.out.println("-------------");
        System.out.println();
    }

    public void DisplayNoLF() {
        int index;
        System.out.println("list Contents: ");
        for (index = 0; index < numUsed; index++) {
            System.out.print(storage[index]+" ");
        }
        System.out.println("-------------");
        System.out.println();
    }

    public int Size(){
        return numUsed;
    }
}

みんなありがとう

4

4 に答える 4

1

It should be

while(index < numUsed)

If you use <=, you'll always access index 0 in an empty list, which will be null. Then when you try to call compareTo on it it will throw an NPE.

ALso, if null is a legal value to add to your list, you'll need to put a null check around the compareTo call and decide if null is alphabetically first or last.

于 2013-02-15T19:10:54.643 に答える
1

Arrays#sort注文を維持するために使用できます。ライブラリで既に利用可能です。

public boolean Insert(String value){
    boolean result = false;

    if (numUsed < length) {
        storage[index] = value;
        numUsed++;
        result = true;
        Arrays.sort(storage); 
    }
    return result;
}
于 2013-02-15T19:15:43.203 に答える
0

Here's why:

public boolean Insert(String value){
    boolean result = false;
            int index = 0;
            if (numUsed < length) {
                while (index <= numUsed) { // Here the first time numUsed = 0, index = 0, index <= numUsed;
                    int compare = storage[index].compareTo(value); // The first time, storage[0] == null; NullPointException is thrown
                    if (compare < 0)
                        index++;
                }
                moveItemsDown(index);
                storage[index] = value;
                numUsed++;
                result = true;
            }
            return result;
}

Maybe change <= to < will do?

于 2013-02-15T19:10:34.590 に答える
0

最初にこの行を実行します

int compare = storage[index].compareTo(value);

storage[index]あなたはどの値がどの値であるかを比較しnullていますvaluenot null or empty

スムーズな交換を可能にする

          while(index =< numUsed) {
        int compare = storage[index].compareTo(value);
        if(compare < 0)
            index++;
    }

 while(index < numUsed) {
        int compare = storage[index].compareTo(value);
        if(compare < 0)
            index++;
    }
于 2013-02-15T19:26:11.733 に答える