-2

I'm running into an issue with my code and I can't figure out what's wrong. Basically the project I'm working on wants me to use static methods to perform a few different tasks using arrays. What I'm stuck on now, specifically, is printing my array.

The first thing I had to do was ask the user for the size of their array, and then have them input the data. After, I'm supposed to take the minimum value and maximum value, and then swap them. I'm pretty sure my code it right, but when I call the method I'm running into an error. This is what I have so far and I keep getting an error on lines 42 (beginning of swap method), 66 and 69:

public class ArraysStaticMethods {
    static int[] array;
    static public int arraySize;
    static public int max;
    static public int min;

    //will create an array with user's input
    private static int[] readInputs(int arraySize){
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter the length of your array:  ");
        arraySize = keyboard.nextInt();
        array = new int[arraySize];

        for (int i = 0; i <= array.length - 1; i++) {
        System.out.print("Enter an integer: ");
            int num = keyboard.nextInt();
            array[i] = num;
        }

          //to test
        System.out.println("Your array before:");
        System.out.println(Arrays.toString(array));

        return array;
    }

    //finds index of max and min values and swaps them
    public static int[] swap(int[] array){
        max = array[0];
        min = array[0];
        int maxIndex = 0;
        int minIndex = 0;
        for(int index = 1; index < array.length; index++){
            if (array[index] > max){
                max = array[index];
                maxIndex = index;
            } 
            if (array[index]<min) {
                min = array[index];
                minIndex = index;
            }
        }
        array[maxIndex] = min;
        array[minIndex] = max;

        System.out.println("Your array after:");
        System.out.println(Arrays.toString(array));
        return array;
    }

    public static void displayOutputs(){
        readInputs(arraySize);
        swap(array);
    }

    public static void main(String[] args) {
        displayOutputs();
    }
}
  • 2
    You forgot to tell us what errors you're getting and on what lines you're getting them. – David Nov 17 '16 at 18:36
  • The errors are on lines 42, 66, and 69 and it says "exception in thread "main" java.lang.NullPointerException at" and then those lines. – A. Brian Nov 17 '16 at 18:37
  • 2
    @A.Brian http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – denvercoder9 Nov 17 '16 at 18:39

1 Answers1

3

You're never actually assigning the result of readInputs() to array, since you are hiding it inside that method. You're returning the local version, but not using that one either.

Instead of

int[] array = new int[arraySize];

at line 17, just do

array = new int[arraySize];

OR you can change your displayOutputs() method to assign the result of readInputs() to array, before passing it to swap().

Isabelle Plante
  • 518
  • 3
  • 7
  • 20