Why am I getting “NullPointerException” when accessing an array element in Java?
17:24 23 Nov 2025

I am learning Java and I’m getting a NullPointerException when I try to access an element of an array.

Here is my code: int[] arr = null; System.out.println(arr[0]); When I run this, I get the following exception: Exception in thread "main" java.lang.NullPointerException

nullpointerexception