为什么我的列表不为null时会出现java.lang.NullPointerException?

时间:2019-03-10 05:19:30

标签: java nullpointerexception

我的代码中的所有内容都可以正常工作,直到我走到最后并希望对列表进行排序。驱动程序和方法都可以正常编译,并且存在arraylist,因为它用于驱动程序中的所有其他方法,但是当我对列表进行排序时,我得到了一个错误。

这是驱动程序的结尾部分

        intList.removeAt(position);                                 //Line 31
    System.out.println("Line 32: After removing the "
                      + "element at position "
                      + position
                      + ", intList:");                          //Line 32

    intList.print();                                            //Line 33
    System.out.println();                                       //Line 34

    System.out.print("Line 35: Enter the search "
                    +  "item: ");                               //Line 35
    System.out.flush();                                         //Line 36

    num.setNum(Integer.parseInt(keyboard.readLine()));          
    System.out.println();                                       

    if(intList.seqSearch(num) != -1)                            
       System.out.println("Line 40: Item found in "
                        + "the list");                          
    else                                                        
       System.out.println("Line 42: Item not found");           

    System.out.print("List 43: The list temp: ");               
    intList.sortList();                                  //line 91 (problem line)       
    System.out.println(); 
 }
}

这是方法:

public class OrderedArrayList extends UnorderedArrayList
{

public OrderedArrayList(int size)
{
      super(size);
}

public OrderedArrayList()
{
    super();
}

    //Copy constructor
public OrderedArrayList(OrderedArrayList otherList)
{
    super(otherList);
 }

 public void sortList()
 {// start sort
 list.print();
  int min, i, j;
   DataElement temp;
   for (  i = 0; i < list.length; i++) 
   {// start for
    // Assume first element is min
    min = i;
    for (  j = i + 1; j < list.length; j++)
     {                                        // line 30 (other problem line)
        if (list[j].compareTo (list[min])<0) 
        {
            min = j;

        }
     }
    if (min != i)
     {
        temp = list[i];
        list[i] = list[min];
        list[min] = temp;
     }
    System.out.println(list[i]); 
    }


 }// end sort

}

这是我的输出,包括错误:

 ----jGRASP exec: java -ea Example3_1
Line 7: Processing the integer list
Line 8: Enter 8 integers on the same line: 1 5 8 7 4 6 9 2

Line 16: The list you entered is: 1 5 8 7 4 6 9 2 

Line 19: Enter the num to be deleted: 1

Line 24: After removing 1 the list is:
5 8 7 4 6 9 2 

Line 27: Enter the position of the num to be deleted: 1

Line 32: After removing the element at position 1, intList:
5 7 4 6 9 2 

Line 35: Enter the search item: 5

Line 40: Item found in the list
List 43: The list temp: Exception in thread "main" java.lang.NullPointerException
at OrderedArrayList.sortList(OrderedArrayList.java:30)
at Example3_1.main(Example3_1.java:91)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

1 个答案:

答案 0 :(得分:0)

虽然我没有编写代码来测试我的建议,但是我观察到两件事。首先,您需要测试列表不为null或为空,如果此条件为true,则返回。 'removeAt'行将删除列表中的一个项目,并且由于未显示列表中项目的打印语句,因此我无法确定它是否为空。当从不可用空间访问时,空列表将导致NullPointer异常。其次,您需要将swap块移到内部for循环中,并且在min之后刚好设置为j的if语句中进行比较。

一些示例排序算法可用。其中一个链接是http://www.java2novice.com/java-sorting-algorithms/quick-sort/

祝你好运。