Dr.Sys
Dr.Sys

Reputation: 15

Error while trying to get max value in array

So I was trying to return the max value within an type "T" array list and got an error while using compareTo. This is the full code.

package myUtil;

public class SimpleListAry<T extends java.lang.Comparable<T>> extends java.lang.Object implements SimpleList<T> {
    private T[] myList;
    private int size;
    public SimpleListAry(){
       myList = (T[])new Comparable[10];
    }
    public SimpleListAry(int capacity){
        if (capacity <= 0){
            throw new IllegalArgumentException();
        }
        myList = (T[]) new Object [capacity];
    }

    @Override
    public int size() {
        size = myList.length;
        return size;
    }
    @Override
    public T get(int i) {
        return myList[i];
    }

    @Override
    public T set(int i, T item) {
        return myList[i] = item;
    }

    @Override
    public int indexOf(Object item) {
        for (int i = 0; i < size; i++){
            if (get(i).equals(item)){
                return i;
            }
        }
      return -1;
    }

    @Override
    public void add(int at, T item) {
        if (at < 0 || at > size)
            throw new ArrayIndexOutOfBoundsException(at);
        for (int i = size; i > at; i--){
            myList[i] = myList [i-1];
        }
//        myList[at] = item;
        size++;
    }


    @Override
    public T remove(int at) {
        if (at < 0 || at >= size)
            throw new ArrayIndexOutOfBoundsException(at);
        T item = myList[at];
        for (int i = at; i<size-1; i++)
            myList[i] = myList[i+1];
        size--;
        return item;
    }

    @Override
    public T max() {
        T max = myList[0];
        for (int i = 1; i < myList.length; i++){
            if(myList[i].compareTo(max) == 1)
                max = myList[i];
        }
        return max;
    }

    @Override
    public T min() {
        T min = myList[0];
        for (int i = 1; i < size -1; i++){
            if (myList[i].compareTo(min) == -1)
                min = myList[i];
        }
        return min;
    }


}

and the error is at Public T max():

public T max() {
T max = myList[0];
for (int i = 1; i < myList.length; i++){
    if(myList[i].compareTo(max) == 1)
        max = myList[i];
}
return max;

}

I also tried using ">" to compare them but that was not working either. It may be because of the data type but there's no error in the IDE only when I attempt to run it and it points directly to this line in T max(){

if(myList[i].compareTo(max) == 1)

Upvotes: 0

Views: 78

Answers (1)

user85421
user85421

Reputation: 29710

Three possibilities of null pointer in if(myList[i].compareTo(max) == 1)

  1. myList - being initialized in constructor, not null
  2. myList[i] - list initially filled with null !
  3. max, depending on how compareTo is implemented - initialized to myList[0], can be null if list empty

The second case is the problem since the elements of the whole underlying array are being compared without considering the real size of the list.

The size() method is wrong since it sets the size variable, which is returned, to the length of the array overwriting the correct value.

Just remove the assignment statement inside the size() method (and use that method in the comparison loop)

Upvotes: 1

Related Questions