如何在For循环中修复NullPointerException

时间:2017-10-04 02:27:43

标签: java for-loop nullpointerexception

所以我对Java有点新意,只做了几个月。我正在尝试实现不同类型的排序算法,我认为一切都没问题,除非我运行它,我在for循环上得到一个NullPointerException。我已经阅读了这篇文章What is a NullPointerException, and how do I fix it?。但即使我认为这很简单,我仍然无法弄清楚如何解决问题。有什么帮助吗?

import java.lang.*;
import java.util.*;

public class SortApp {

    public int[] generateRandomArray(int size) {

        int[] output = new int[size];
        for (int i = 0; i < size; i++) {
            output[i] = (int) (Math.random() * Integer.MAX_VALUE);
        }
        return output;
    }

    public void printArray(int[] inarray) {

        for (int i = 0; i < inarray.length; i++) {
            System.out.println(inarray[i]);
        }
    }

    public boolean isSorted(int[] inarray) {
        for (int i = 0; i < inarray.length; i++) {       //Error here
            if (inarray[i] > inarray[i + 1]) {
                return false;
            }
        }
        return true;
    }

    public int[] insertionSort(int[] inarray) {

        int[] data = Arrays.copyOf(inarray, inarray.length);
        int temp;
        int j;
        for (int i = 0; i < inarray.length; i++) {
            temp = inarray[i];
            for (j = 0; (j > 1) && (temp < inarray[j - 1]); j--)
                inarray[j] = inarray[j - 1];
            inarray[j] = temp;
        }
        return data;
    }

    public int[] selectionSort(int[] inarray) {

        int[] data = Arrays.copyOf(inarray, inarray.length);
        int temp;
        int n = inarray.length;
        for (int i = 0; i < inarray.length; i++) {
            int k = i;
            for (int j = i + 1; j < n; j++)
                if (inarray[j] < inarray[k])
                    k = j;
            temp = inarray[i];
            inarray[i] = inarray[k];
            inarray[k] = temp;
        }
        return data;
    }

    public int[] mergeSort(int[] inarray, int temp[], int l, int r) {

        int[] data = Arrays.copyOf(inarray, inarray.length);

        if (l == r) return null;
        int mid = (l + r) / 2;
        mergeSort(inarray, temp, l, mid);
        mergeSort(inarray, temp, mid + 1, r);

        int i, j, k;
        for (i = l; i <= r; i++)
            temp[i] = inarray[i];
        for (i = l, j = mid + 1, k = l; i <= mid && j <= r && k <= r; k++)
            if (temp[i] < temp[j]) inarray[k] = temp[i];
        i++;
        inarray[k] = temp[j];
        j++;
        for (; i <= mid; i++, k++)
            inarray[k] = temp[i];
        for (; j <= r; j++, k++)
            inarray[k] = temp[j];

        return data;
    }

    public static void main(String[] args) {
        SortApp myApp = new SortApp();
        int[] data = myApp.generateRandomArray(10);

        boolean isSortedA = myApp.isSorted(myApp.insertionSort(data));
        boolean isSortedB = myApp.isSorted(myApp.selectionSort(data));
        boolean isSortedC = myApp.isSorted(myApp.mergeSort(data, data, 0, 0)); //Error here
        myApp.printArray(data);
    }
}

我得到的错误是:

Exception in thread "main" java.lang.NullPointerException
at SortApp.isSorted(SortApp.java:23)
at SortApp.main(SortApp.java:91)

1 个答案:

答案 0 :(得分:1)

这是因为这一行:

if (l==r) return null;

你传递0表示l和r意味着它们是相等的。因此,您将获得传入isSorted()调用的空值。