NullPointerException
What is NullPointerException?
NullPointerException: The code tries to use an object reference that points to nothing (null). Mostly a Java / Kotlin / C# concept. A NullPointerException in Java, Kotlin or C# happens when code tries to use an object reference that points to nothing. Calling user.getName() when user is null is the classic example. PHP's equivalent is "Call to a member function on null".
Frequently asked questions about NullPointerException
How is NullPointerException used in practice?
The stack trace shows where it crashed, but the real bug is usually earlier, where the null value came from. A lookup that found nothing, an optional field left empty, or a value never initialised.
What should you keep in mind with NullPointerException?
Fix the source rather than adding null checks everywhere. Make it explicit which values may be null, handle the empty case where data enters the system, and use language features that help. Kotlin's type system separates nullable and non-null types, Java has Optional, and in PHP, nullable types plus static analysis catch most cases before they reach production.
At a glance
| Category | Error types |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |