-2

everything in my code works fine till I get to the end and want to sort the list. the driver and the method both compile fine and the arraylist is present since it is used for all the other methods in the driver but when I go to sort the list I get an error.

this is the end section of the driver

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

here is the method:

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

}

and this is my output including the error:

 ----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.
  • What line does the exception occur on per the stacktrace? `at OrderedArrayList.sortList(OrderedArrayList.java:30)` -- line 30. What line don't you show us? -- line 30. – Hovercraft Full Of Eels Mar 10 '19 at 05:24
  • 1
    But having said this, the heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Mar 10 '19 at 05:25
  • Since the first statement inside `sortList()` is `list.print();` and nothing is printed, I would guess that to be line 30, and it would appear that `list`, whatever it is, is null. Without source to `UnorderedArrayList`, where presumably `list` is declared, it's impossible to help further. – Andreas Mar 10 '19 at 05:31
  • I figured it out through a lot of frustration. turns out I was trying to sort an arraylist but had the sort method set for sorting an array so it was coming up null – Steven Anderson Mar 11 '19 at 01:38

1 Answers1

0

While I haven't written code to test out my suggestions, I have observed two things. First, you need to test that your list is not null or empty and return if this condition is true. The 'removeAt' line removes an item in the list and because the print statement of items in the list is not shown, I cannot tell if it is empty. Empty lists will result in a NullPointer exception when accessed from an unavailable space. Second, you need to move the swap block into the inner for-loop and within the comparison if statement just after min is set to j.

Some example sorting algorithms are available. One such link is http://www.java2novice.com/java-sorting-algorithms/quick-sort/.

Good luck.

CyrilDex
  • 137
  • 1
  • 6
  • I updated my question to show all the output. it has data in the list and every thing functions fine except the sort. im guessing there is some error when calling the method in the OrderedArrayList but I am unable to figure out where that part went wrong – Steven Anderson Mar 10 '19 at 06:25