简体   繁体   English

不知道为什么我会收到NullPointerException错误

[英]Not sure why I'm getting a NullPointerException error

So I'm running a program that does various things to a String array. 因此,我正在运行一个对String数组执行各种操作的程序。 One of them is a inserting a string inside the array and sorting it. 其中之一是在数组内插入字符串并对其进行排序。 I'm able to use the sort method, but when I attempt to insert a string and then sort it I get a NullPointerException. 我可以使用sort方法,但是当我尝试插入一个字符串然后对其进行排序时,我得到了NullPointerException。 This is the code: 这是代码:

    import java.util.Scanner;
    import java.io.*;

    public class List_Driver
    {
        public static void main(String args[])
        {
            Scanner keyboard = new Scanner(System.in);
            int choice = 1;
            int checker = 0;
            String [] words = new String[5];
            words[0] = "telephone";
            words[1] = "shark";
            words[2] = "bob";
            ListWB first = new ListWB(words);
            int menu = uWB.getI("1. Linear Seach\n2. Binary Search\n3. Insertion             in Order\n4. Swap\n5. Change\n6. Add\n7. Delete\n8. Insertion Sort\n9. Quit\n");
            switch(menu)
            {
                //other cases
                case 3:
                {
                    String insert = uWB.getS("What term are you inserting?");
                    first.insertionInOrder(insert);
                    first.display();
                }//not working
                break;

                }//switch menu
        }//main
    }//List_Driver

uWB is a basic util driver. uWB是基本的util驱动程序。 It doesn't have any problems. 它没有任何问题。 This is the ListWB file itself: 这是ListWB文件本身:

    public class ListWB
    {
    public void insertionSort()
        {
            for(int i = 1; i < size; i++)
        {
        String temp = list[i];
        int j = i;
        while(j > 0 && temp.compareTo(list[j-1])<0)
        {
            list[j] = list[j-1];
            j = j-1;
        }
        list[j] = temp;
        }
    }
    public void insertionInOrder(String str)
    {
            insertionSort();
        int index = 0;
        if(size + 1 <= list.length)
        {
            while(index < size && str.compareTo(list[index])>0)
                    index++;
            size++;
            for (int x = size -1; x> index; x--)
                list[x] = list[x-1];
            list[index] = str;
        }
        else 
            System.out.println("Capacity Reached");
    }//insertioninorder
}//ListWB

How would I fix this? 我该如何解决?

You have an array of 5 Strings, but only 3 of them initialized. 您有5个字符串的数组,但是只有3个被初始化。 The rest points to null (because you did not initialize them): 其余的都指向null(因为您没有初始化它们):

  String [] words = new String[5];
  words[0] = "telephone";
  words[1] = "shark";
  words[2] = "bob";
  words[3] = null;
  words[4] = null;

The first line only initializes the array itself, but not the containing objects. 第一行仅初始化数组本身,而不初始化包含的对象。

But the insert iterates over all 5 elements. 但是插入会迭代所有5个元素。 And temp is null, when i is 3. So the statement temp.compareTo throws an NullPointerException. 当我为3时,temp为null。因此,语句temp.compareTo抛出NullPointerException。

 for(int i = 1; i < size; i++)
    {
    String temp = list[i];
    int j = i;
    while(j > 0 && temp.compareTo(list[j-1])<0)

Solution: Also check temp for null in the while loop. 解决方案:同时在while循环中检查temp是否为null。 Or do not use a string array at all but a auto-resizable data structure list java.util.ArrayList. 或者根本不使用字符串数组,而是使用可自动调整大小的数据结构列表java.util.ArrayList。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM