質問

I wrote a java to test a insertion sort for strings, but it comes up with error saying "java.lang.NullPointerException:

import java.util.Arrays;
public class SortTest
{
    private String[] array;
    final int size = 5;

    public void sort()  
    {
    String insert;
    array = new String[size];


    for ( int next = 1; next < array.length; next++ )
    {
        insert = array[next];

        int moveItem = next;

        while  (moveItem > 0 && array[moveItem -1].compareTo(insert) > 0)
        {
            array[moveItem] = array[moveItem -1];
            moveItem--;
        }
        array[moveItem] = insert;
        }
    }
    public static void main (String[] args) 
    {
    SortTest stringSort = new SortTest();
    String array[] = {"aaa", "ccc", "eee", "zzz", "bbb"};

    stringSort.sort();
    System.out.println( stringSort );
     }
 }

I followed most of the codes from textbook and I really can't find where the problems are, please give me some help! Any help is appreciated!

役に立ちましたか?

解決 2

You are not passing the array object to sort method. Try the following code:

public class SortTest {

    public void sort(String[] array) {

        String insert;

        for (int next = 1; next < array.length; next++) {
            insert = array[next];

            int moveItem = next;

            while (moveItem > 0 && array[moveItem - 1].compareTo(insert) > 0) {
                array[moveItem] = array[moveItem - 1];
                moveItem--;
            }
            array[moveItem] = insert;
        }
    }

    public static void main(String[] args) {
        SortTest stringSort = new SortTest();
        String[] array = { "aaa", "ccc", "eee", "zzz", "bbb" };
        //call sorting
        stringSort.sort(array);
        //print the sorted array values
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}

他のヒント

You initialize your array as new String[size];.

This will populate a String array of size size, with default values, aka null for Strings.

When you call compareTo with items of your array, you are referencing a null value as your array was not populated with String instances prior to that.

As a result, you get a NullPointerException.

As a quick test, try the following code:

String[] foo = new String[2];
System.out.println(Arrays.toString(foo));

The output will be:

[null, null]

You forgot couple of things .. you are not passing your array to your sort() method . Second In sort method you are initializing new array that contains null value . So you need to change two things..

public class SortTest {
    String[] array;
    final int size = 5;
    public SortTest(String[] array) {
        this.array = array;
    }
    public void sort() {
        String insert;
        // array = new String[size];
        for (int next = 1; next < array.length; next++) {
            insert = array[next];
            int moveItem = next;
            while (moveItem > 0 && array[moveItem - 1].compareTo(insert) > 0) {
                array[moveItem] = array[moveItem - 1];
                moveItem--;
            }
            array[moveItem] = insert;
        }
    }

    public static void main(String[] args) {
        final String array[] = { "aaa", "ccc", "eee", "zzz", "bbb" };
        final SortTest stringSort = new SortTest(array);
        stringSort.sort();
        System.out.println(stringSort);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top