Troubleshooting NullPointerException

Q

What is NullPointerException and how to handle it?

✍: FYIcenter

A

When an object is not initialized, the default value is null. When the following things happen, the NullPointerException is thrown:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of a null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

The NullPointerException is a runtime exception. The best practice is to catch such exception even if it is not required by language design.

2007-03-03, 8017👍, 0💬