A-Z Popular Blog Coding Search »
Related Guides

4 Examples of Null Pointer Exceptions

 , updated on
Null pointer exceptions are runtime errors that are generated when you try to use an object that hasn't been initialized. This is one of the most common types of exceptions in many programing languages. Fortunately, it is also one of the easiest to deal with.


Before you use any object test if it is null if there is any possibility that it is uninitialized. If it is null, do something intelligent given the context. For example, if you query a database and the result is null, this may indicate an empty result set. As such, you might display a meaningful message to users that no results were found.


A null pointer exception indicates an object was used before being initialized. Troubleshooting such problems involves looking at the line where the error occurred to identify where the code uses an object without testing if it is null.


A null pointer exception indicates that code attempted to access an object that points at nothing. Generally speaking, it is a poor practice to throw your own null pointer exception because this has little global meaning. For example, if your code can't connect to a database, it is better to throw an exception that indicates this fact as opposed to throwing a null pointer exception. If a required parameter is null, throwing an exception such as "IllegalArgumentException" is more helpful.


When null pointer exceptions do occur it is common to handle them by throwing them such that they result in some type of error to the user. As they are an extremely common and generic type of exception, it is difficult to make assumptions about what they mean. For example, it would typically look sloppy to implement business logic to handle a null pointer exception. In some cases, a null pointer exception is handled with an empty catch or a log entry. Generally speaking, it is better to prevent the exception in the first place as this makes your code more clear to others.
Overview: Null Pointer Exceptions
Runtime errors that are generated when code attempts to use an uninitialized object.
Related Concepts
More about coding:
Code Refactoring
Code Smell
Complexity Hiding
Deep Magic
Edge Case
Event Processing
Forward Compatibility
IT Artifact
IT Examples
Negative Code
Proof Of Work
Software Design
More ...
If you enjoyed this page, please consider bookmarking Simplicable.


A list of coding considerations and techniques.

Negative Code

An overview of negative code.


The definition of synchronous with examples.

Division By Zero

A complete overview of division by zero and why it has no meaning.


An overview of null with examples.

Linux Processes

The basic types of linux process.


A quick reference of useful ls commands.

Linux Commands

A list of unusually useful linux commands with brief explanations.

Runtime Error

The definition of runtime error with examples.


Examples of computing in everyday life.

Job Processing

A definition of job processing with examples.

System Architecture

Examples of system architecture diagrams.


The definition of cloud-scale with examples.

Vertical Scale

The definition of vertical scale with an example and comparison to horizontal scale.

Network Infrastructure

A list of common types of network infrastructure.

Root Access

A complete guide to root access.


The common types of modern computer.
The most popular articles on Simplicable in the past day.

New Articles

Recent posts or updates on Simplicable.
Site Map