-1

I am writing java code for sorting an Integer array using my own custom Comparator, the code I written has a block :

Arrays.sort(arrI, new Comparator<Integer>()
{

    @Override
    public int compare(Integer o1, Integer o2) {

        return o2.compareTo(o1);
    }
});

Where I'm getting a NullPointerException at this line :

return o2.compareTo(o1);

The reason is perhaps in the method where I'm taking the arguments are just the reference variables (they haven't been initialized with the object value). But knowing that I can't do anything. I'm simply over riding the method which I don't know how and when it is called. So I can't actually solve this problem.

How can I remove the exception?

My complete code is:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.*;

public class Prob1 {

    public static void main(String []args)
    {
        int arr[] = new int[10+5];
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; ++i)
        {
            arr[i] = scanner.nextInt();
        }
        Integer arrI[] = new Integer[arr.length];
        int i = 0;
        for(int value  = 0; value < n; ++value)
        {
            arrI[value] = Integer.valueOf(arr[value]);
        }
        Arrays.sort(arrI, new Comparator<Integer>()
        {
            @Override
            public int compare(Integer o1, Integer o2) {

                return o2.compareTo(o1);
            }
        });
        for (int k = 0; k < arrI.length; ++k)
        {
            System.out.println(arrI[k]);
        }       
    }

}
Ayushi bhardwaj
  • 441
  • 5
  • 18
  • The problem with arrays in java is: they are not dynamic. You need to initialize each and every element your own. Therefore your code only works, when the users first input is 15 because that's the size you declared your array for. Either move the array definition behind the first input (using that as array size) or even better use a `Collection`, an `ArrayList` in ths case. – Timothy Truckle Nov 20 '16 at 11:18

2 Answers2

3

arr[] has length of 15 and later if user enter n as 10 so your arr will have 10 elements (last five as 0)and so Integer arrI[] = new Integer[arr.length]; will have 15 elements where last 5 will be null so do this

 Integer arrI[] = new Integer[n];

Initializing arr with 15 length seems like isn't the requirement of your code logic so you can simply avoid this by using n at first place as

    int arr[];                // declaration of array
    Integer arrI[];
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    arr = new int[n]         // initialization of array
    .
    .
    arrI[] = new Integer[n]; 
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • 1
    thanks but can you tell why was it giving exception at this line : `return o2.compareTo(o1);`? – Ayushi bhardwaj Nov 20 '16 at 11:25
  • 1
    @Ayushibhardwaj as i said `last 5 elements will be null` so in-short `null` is not a class and has no implementation of `compareTo` (makes no sense comparing a object with a null or null with a null) so hence exception – Pavneet_Singh Nov 20 '16 at 11:29
1

arrI array has a length of 15 since you do :

   int arr[] = new int[10+5];
   ...
   Integer arrI[] = new Integer[arr.length];

And here you iterate until n, a input of the user which may be different from 15 :

    int n = scanner.nextInt();
    ....

    for(int value  = 0; value < n; ++value)
    {
        arrI[value] = Integer.valueOf(arr[value]);
    }

So arrI may have null value in this array if n < arrI.length and if n > arrI.length, arrI[value] will throw ArrayIndexOutOfBoundsException.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • thanks but can you tell why was it giving exception at this line : `return o2.compareTo(o1);`? – Ayushi bhardwaj Nov 20 '16 at 11:25
  • Because you have null objects in the list. It is explained in the answer : **arrI may have null value in this array if n < arrI.length** and `arrl` is the list you use when your sort. – davidxxx Nov 20 '16 at 11:26