1

I'm trying to figure out why I'm getting a NullPointerException despite the fact that the array is populated with card object. What I'm asking is that can my QuickSort algorithm use the compare method I override in my card class?

QuickSort:

package sort;

//All import goes here
import java.util.ArrayList;
import java.util.Comparator;

//Comment out at the moment
//import java.util.List;



public class QuickSort<T> {

    private ArrayList<T> list;
    private int length;
    Comparator<T> pod;



    public void sort(ArrayList<T> lst){

        //Check if the list is empty
        //if(lst.isEmpty() || lst == null){
        if(lst == null || lst.isEmpty()){
             return;
        }
        this.list = lst;
        length = lst.size();
        quickSort(0, length - 1);

    }

    private void quickSort(int lowIndex, int highIndex){
        int low = lowIndex;
        int high = highIndex;

        //Find the pivot value
        T pivot = list.get(lowIndex + (highIndex - lowIndex)/2);

        while(low <= high){
            /**
             * In each iteration, we will find a value from the left side 
             * which is greater than the pivot value, and also we will
             * find a value from the right side which is less then the pivot 
             * once the search is done, we will then exchange both numbers 
             */

            //o1 < pivot
            while(pod.compare(list.get(low), pivot) == -1){

                low++;

            }

            //o1 > pivot
            while(pod.compare(list.get(high), pivot) == 1){
                high--;
            }

            if(low <= high){
                swap(low, high);

                //Move index to next position on both side 
                low++;
                high--;
            }
        }

        //Call quickSort recursively
        if(lowIndex < high){
            quickSort(lowIndex, high);
        }
        if(low < highIndex){
            quickSort(low, highIndex);
        }

    }

    private void swap(int i, int j){
        //Temporary variable to hold the element at index i 
        T temp = list.get(i);
        //Swap i index with the element in j index
        list.set(i, list.get(j));
        //Swap j index with the element that was in i index
        list.set(j, temp);
    }
}

Card:

//Compare method in my Card class
public class Card implements Comparator<Card>{
    .....

    @Override
    public int compare(Card o1, Card o2) {

        //Check if o1 has the highest suit
        if(o1.getSuit().getVal() > o2.getSuit().getVal()){
            return 1;
        }

        //Check if o2 has highest suit
        else if(o1.getSuit().getVal() < o2.getSuit().getVal()){
            return -1;
        }

        //Check if the suit are the same
        else if(o1.getSuit().equals(o2.getSuit())){

            //Check if o1 has the highest value
            if(o1.getValue().getVal() > o2.getValue().getVal()){
                return 1;
            }

            //Check if o2 has the highest value
            else if(o1.getValue().getVal() > o2.getValue().getVal()){
                return -1;
            }
        }
        //Either there duplicate or something wrong with the compare method. 
        System.out.println("Well there something wrong with the card Compare method or there duplicate of card in the deck.");
        return 0;
    }
}

Spade:

//Sort player hand for easy readability
QuickSort<Card> sort = new QuickSort<Card>();
System.out.println(USER.getHand().size());
sort.sort(USER.getHand());

Error:

Exception in thread "main" java.lang.NullPointerException
at sort.QuickSort.quickSort(QuickSort.java:48)
at sort.QuickSort.sort(QuickSort.java:28)
at Spade.main(Spade.java:155)

Update:

I check if the argument were null for the compare method where the exception originate, both argument check return false.

awesoon
  • 26,766
  • 9
  • 62
  • 86

1 Answers1

0

I see two places for null pointer exception:

line 16: pod is not initialized, so it is causing an error at line 48

line 23: test for null before testing isEmpty 

The generic type T must implement comparable, then use pivot.compareTo

Check out this question, it is a generic bubble sort. Generic arraylist bubblesort problems

Community
  • 1
  • 1
downeyt
  • 856
  • 2
  • 9
  • 21