When does a NullPointerException get thrown in java?



A null pointer exception is thrown Whenever you try to

  • Call the instance method of a null object.
  • Access, modify, print, a null value.
  • Trying to access (print/use in statements) the length of the null value.
  • Throw a null value.
  • Access an element of an array without initializing it.

Then, a Null Pointer Exception will be thrown.


Advertisements