0

I've got java.lang.ArrayIndexOutOfBoundsException: null exception in my application two times, both of them happened at list.get() method, the JDK source like this

 `private E get(Object[] paramArrayOfObject, int paramInt)
  {
    return paramArrayOfObject[paramInt];
  }
  `

What's the problem? please give some simple example which throws java.lang.ArrayIndexOutOfBoundsException null.
Thanks!

Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
Richard
  • 31
  • 1
  • 1
  • 1
    The answer is actually in the name of the exception. The array index is out of bounds. So there is no element at that index. – sleepToken Dec 26 '19 at 12:32
  • my guess is that you passed `paramInt` < 0 – Eran Dec 26 '19 at 12:33
  • *both of them happened at list.get() method...* It usually happen at `.get()` calls only. – Vishwa Ratna Dec 26 '19 at 12:34
  • 1
    I think anyone new to programming both Null pointer exception and Array Out of Bound exception are different situation for them if anyone found duplicate then question must be closed with proper LINK.. – Kandy Dec 26 '19 at 12:49

1 Answers1

1

Please check array length before calling paramArrayOfObject[paramInt];

if(paramArrayOfObject.length > paramInt) return paramArrayOfObject[paramInt]; else return null;

Jaganath Kamble
  • 506
  • 2
  • 10
  • 2
    thanks, but the code is from jdk source code, not my code and if the index is out of the array range, it will throw java.lang.ArrayIndexOutOfBoundsException:7 like this, not "null" . – Richard Dec 27 '19 at 01:20