-2

I am new to java. I wrote a program to sort an array using bubblesort algorithm.I have 3 methods getArray(), putArray() & sortArray(). I have called all these methods from the main function passing the no of elements and an array as parameters. I have passed a null array to getArray() function and got the array as input from the user. When i get the input i get exception. Kindly help me out. I am literally stranded.`

import java.util.Scanner;
public class BubbleSort {
    public void getArray(int num,int[]arr) {
        System.out.print("Enter the total number of elements in the Array :  ");
        Scanner sc = new Scanner(System.in);
        num = sc.nextInt();
        System.out.println(num);
        System.out.print("Enter " + num + " Elements  : ");
        for (int i = 0; i < num; i++) {
            arr[i] = sc.nextInt();
        }
    }
    public void putArray(int num,int[] arr) {
        System.out.print("The Array is:  ");        
        for (int i = 0; i < num; i++) {
            System.out.println(arr[i] + "  ");
        }
    }
    public void sortArray(int num,int[] arr) {
        for (int i = 0; i < num; i++) {
            boolean flag = false;
            for (int j = 0; j < num - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    arr[j] = arr[j] + arr[j + 1];
                    arr[j + 1] = arr[j] - arr[j + 1];
                    arr[j] = arr[j] - arr[j + 1];
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }
    public static void main(String[] args) {        
        int num=0;
        int[] arr=null;
        BubbleSort b = new BubbleSort();
        b.getArray(num,arr);
        b.putArray(num,arr);
        b.sortArray(num,arr);
        b.putArray(num,arr);
    }
}
TimeToCode
  • 901
  • 2
  • 16
  • 34
Harish93
  • 1
  • 4

1 Answers1

0

Its because you are passing null array and trying to set input in that null array.

for (int i = 0; i < num; i++) {
                arr[i] = sc.nextInt();
            }

Here, in above code, arr is null and adding value to 0 index will throw nullpointer exception. So, you must initialize array according to given num size. i.e.

arr = new int[num];

Rewritten function:

public class BubbleSort {
    private int num;

public int[] getArray() {
    System.out.print("Enter the total number of elements in the Array :  ");
    Scanner sc = new Scanner(System.in);
    num = sc.nextInt();
    System.out.println(num);

    int[] arr = new int[num];

    System.out.print("Enter " + num + " Elements  : ");
    for (int i = 0; i < num; i++) {
        arr[i] = sc.nextInt();
    }
    return arr;
}

public void putArray(int num, int[] arr) {
    System.out.print("The Array is:  ");
    for (int i = 0; i < num; i++) {
        System.out.println(arr[i] + "  ");
    }
}

public void sortArray(int num, int[] arr) {
    for (int i = 0; i < num; i++) {
        boolean flag = false;
        for (int j = 0; j < num - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                arr[j] = arr[j] + arr[j + 1];
                arr[j + 1] = arr[j] - arr[j + 1];
                arr[j] = arr[j] - arr[j + 1];
                flag = true;
            }
        }
        if (!flag) {
            break;
        }
    }
}

public int getNum() {
    return num;
}

public void setNum(int num) {
    this.num = num;
}

    public static void main(String[] args) {

    int num = 0;
    int[] arr = null;
    BubbleSort b = new BubbleSort();

    arr = b.getArray();
    b.putArray(b.getNum(), arr);
    b.sortArray(b.getNum(), arr);
    b.putArray(b.getNum(), arr);        
}

}
Ra Ka
  • 2,995
  • 3
  • 23
  • 31
  • I changed the code and the exception is gone. but the array is not displaying from putArray() function – Harish93 Nov 04 '16 at 05:21
  • as per my answer, because the value of `num` does not change – Scary Wombat Nov 04 '16 at 05:22
  • its because, arr is initialized inside getArray() function, it will not have the value stored. So, you must return it and use it. I will update the answer. – Ra Ka Nov 04 '16 at 05:29
  • @RaKa. I changed the code according to your latest edit. Still the array is not displaying. Should i change other methods also – Harish93 Nov 04 '16 at 05:43
  • Okay, same thing goes for num variable as well. Please, consider understanding java function passby value and pass by referrence. Also, I have updated the modified answer. – Ra Ka Nov 04 '16 at 05:53