java - Exception Conditions: Incorrect Exception thrown -


i'm having issue homework assignment of mine. have our assignments graded website online , keep receiving following error code have displayed below.

error:

 exception conditions. incorrect exception thrown null a.java.lang.nullpointerexception 
  public static int[] nearestk(int[] a, int val, int k) {    int x = 0;    if (k < x || a.length == 0 || == null)   {      throw new illegalargumentexception("k not invalid");   }    if (k == 0 || k > a.length)    {      int[] incorrect = new int[0];      return incorrect ;   }    final int value = val;    integer[] copy = new integer[a.length];    (int = 0; < a.length; i++) {      copy[i] = a[i];   }    arrays.sort(copy,          new comparator<integer>() {                               @override            public int compare(integer o1, integer o2) {               int distance1 = math.abs(value - o1);                int distance2 = math.abs(value - o2);                return integer.compare(distance1, distance2);            }         });    int[] answer = new int[k];    (int = 0; < answer.length; i++) {      answer[i] = copy[i];   }    return answer;  } 

this line failing:

if (k < x || a.length == 0 || == null) 

because a.length throw nullpointerexception before gets chance check a == null.

try changing to:

if (a == null || k < x || a.length == 0) 

so null checked first.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -