【问题标题】:NullPointerException error in Insertion sort [closed]插入排序中的 NullPointerException 错误 [关闭]
【发布时间】:2014-05-15 11:59:21
【问题描述】:

我编写了一个 java 来测试字符串的插入排序,但它出现错误“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 );
     }
 }

我遵循了教科书上的大部分代码,但我真的找不到问题所在,请给我一些帮助!任何帮助表示赞赏!

【问题讨论】:

标签: java arrays nullpointerexception insertion-sort


【解决方案1】:

您将array 初始化为new String[size];

这将填充大小为sizeString 数组,并使用默认值,也就是null for Strings。

当您使用array 的项目调用compareTo 时,您引用的是null 值,因为在此之前您的array 没有填充String 实例。

因此,您会得到一个NullPointerException

作为一个快速测试,试试下面的代码:

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

输出将是:

[null, null]

【讨论】:

    【解决方案2】:

    您没有将数组对象传递给排序方法。试试下面的代码:

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

    【讨论】:

      【解决方案3】:

      您忘记了几件事..您没有将数组传递给您的 sort() 方法。第二在 sort 方法中,您正在初始化包含 null value 的新数组。所以你需要改变两件事..

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多