-2

So I am sort of new to Java and have only been doing it for a couple of months. I'm trying to implement different types of sorting algorithms and I think everything is okay except when I run it I get a NullPointerException on the for loop. I have already read this post What is a NullPointerException, and how do I fix it?. But I still can not figure out how to fix the problem even though I think it is quite simple. Any help?

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

Error I am getting is this:

Exception in thread "main" java.lang.NullPointerException
at SortApp.isSorted(SortApp.java:23)
at SortApp.main(SortApp.java:91)
the_storyteller
  • 2,335
  • 1
  • 26
  • 37
TechRando
  • 327
  • 2
  • 4
  • 14

1 Answers1

1

It's because of this line:

if (l==r) return null;

You are passing in 0 for both l and r meaning they are equal. Therefore, you are getting a null value passed in to your isSorted() call.

user2023608
  • 470
  • 5
  • 18