为什么我的排序实现中有NullPointerException?

时间:2014-05-22 02:38:47

标签: java arrays

我正在完成我的任务,我想为我的数组创建一个排序方法。但是,我的排序方法中出现空指针错误,我不确定原因。这是我的代码。

public Object[] sorted() {
    getSetArray();
    Arrays.sort(setArray);
    return setArray;
}
  

这些是我的错误行java.lang.NullPointerException at   java.util.ComparableTimSort.binarySort(ComparableTimSort.java:232)at at   java.util.ComparableTimSort.sort(ComparableTimSort.java:176)at at   java.util.ComparableTimSort.sort(ComparableTimSort.java:146)at   java.util.Arrays.sort(Arrays.java:472)at   setadt.SetADT.sorted(SetADT.java:280)at   setadt.SetADTDriver_sol.main(SetADTDriver_sol.java:233)

package setadt;
import java.util.*;



public class SetADT {

    private static Random rand = new Random();
    private final int DEFAULT_CAPACITY = 100;
    private final int NOT_FOUND = -1;
    private int size;
    private Object[] setArray;

    public SetADT() {
        size = 0;
        setArray = new Object[DEFAULT_CAPACITY];
    }

    public SetADT(int capacity) {
        size = 0;
        setArray = new Object[capacity];
    }

    public void add(Object newElement) {

        if (size() == setArray.length) {
            resize();
        }
        if(!contains(newElement) == true){
           setArray[size] = newElement;
         size++;   
        }
        //System.out.println(newElement +"\t" + size);
    }

    public void addAll(SetADT set) {

        Object[] temp = set.getSetArray();

        for (int i = 0; i < set.size(); i++) {
            if (!this.containsValue(temp[i])) {
                add(temp[i]);
            }
        }
    }
   public boolean contains(Object element) {

        for (int i = 0; i < size; i++) {
            if (setArray[i].equals(element)) {
                return true;
            }
        }

        return false;
    }

    public boolean containsValue(Object element) {

        for (int i = 0; i < size; i++) {
            if (setArray[i].toString().equals(element.toString())) {
                return true;
            }
        }

        return false;
    }

    public boolean equals(SetADT set) {

        boolean result = false;
        Object element;

        if (size() == set.size()) {
            result = true;
            for (int i = 0; i < size(); i++) {
                element = setArray[i];
                if (!set.contains(element)) {
                    return false;

                }
            }
        }

        return result;
    }

    public boolean isEmpty() {

        if (size == 0) {
            return true;
        } else {
            return false;
        }
    }

    public Object remove(Object element) {

        Object result = new Integer(NOT_FOUND);
        int search = NOT_FOUND;

        if (!isEmpty()) {
            for (int i = 0; i < size() && search == NOT_FOUND; i++) {
                if (setArray[i].equals(element)) {
                    search = i;
                }
            }

            if (search != NOT_FOUND) {

                result = setArray[search];

                setArray[search] = setArray[size - 1];
                setArray[size - 1] = null;
                size--;
            }
        }

        return result;
    }
    public Object removeRandom() {
        int element;
        Object value = new Integer(NOT_FOUND);

        if (!isEmpty()) {

            element = rand.nextInt(size);
            value = setArray[element];

            setArray[element] = setArray[size - 1];
            setArray[size - 1] = null;
            size--;
        }

        return value;
    }
    public int size() {

        return size;
    }
    public SetADT union(SetADT set) {

        SetADT newSet = new SetADT();

        newSet.addAll(this);
        newSet.addAll(set);

        return newSet;
    }

    private void resize() {

        Object[] newArray = new Object[setArray.length * 2];

        for (int i = 0; i < setArray.length; i++) {
            newArray[i] = setArray[i];
        }

        setArray = newArray;
    }

    public Object[] getSetArray() {

        return setArray;
    }

    public String toString() {
        String result = "";

        for (int i = 0; i < size(); i++) {
            if ((i % 10) == 0 && i != 0) {
                result += "\n";
            } 

            result = result + setArray[i].toString() + "\t";
        }
        return result;
    }

    public SetADT intersection(SetADT set) {

        SetADT newSet = new SetADT(); 
        for(int index = 0; index < set.size; index++){
            if(set.contains(set) == set.contains(setArray)){
                newSet.add(set);
            }   
        }
        return newSet;
    }

    public SetADT difference(SetADT set) {

        SetADT newSet = new SetADT();
        for(int index = 0; index < set.size; index++){
            if(set.contains(set) == false){

                newSet.add(set);
            }
        }
        return newSet;
    }

    public Object[] sorted() {
        setArray = getSetArray();
        Arrays.sort(setArray);
        return setArray;
    }

    public static String getObjectArray(Object[] o) {
        String s = "";
        for (int i = 0; i < o.length; i++) {
            s += (o[i] + " ");
        }
        return s;

    }
}

2 个答案:

答案 0 :(得分:1)

NPE的原因是您在数组中存储null。 ComparableTimSort的实现不检查null。

 private static void binarySort(Object[] a, int lo, int hi, int start) {
 214         assert lo <= start && start <= hi;
 215         if (start == lo)
 216             start++;
 217         for ( ; start < hi; start++) {
 218             @SuppressWarnings("unchecked")
 219             Comparable<Object> pivot = (Comparable) a[start];
 220 
 221             // Set left (and right) to the index where a[start] (pivot) belongs
 222             int left = lo;
 223             int right = start;
 224             assert left <= right;
 225             /*
 226              * Invariants:
 227              *   pivot >= all in [lo, left).
 228              *   pivot <  all in [right, start).
 229              */
 230             while (left < right) {
 231                 int mid = (left + right) >>> 1;
 232                 if (pivot.compareTo(a[mid]) < 0)
 233                     right = mid;
 234                 else
 235                     left = mid + 1;
 236             }
 237             assert left == right;
 238 

您必须确保数值中未存储空值。

为此,您必须断言方法add中传递的参数不为null,并在排序为有效大小之前创建容器的副本。

public Object[] sorted() {

    Object[] toSort = Arrays.copyOf(getSetArray(),getSize());
    Arrays.sort(toSort);
    return toSort;
}

答案 1 :(得分:0)

查看setArray的任何元素是否为空。 Arrays.sort()要求排序的数组的每个元素都必须为非空:"All elements in the array must implement the Comparable interface."