A-Z Popular Blog Coding Search »
Coding
 Advertisements
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.

Prevention

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.

Troubleshooting

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.

Throwing

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.

Handling

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
Type
Definition
Runtime errors that are generated when code attempts to use an uninitialized object.
Related Concepts
More about coding:
Abstraction
Algorithms
API
Bootstrapping
Caching
Code Refactoring
Code Smell
Complexity Hiding
Components
Deep Magic
Edge Case
Event Processing
Forward Compatibility
Hardcoded
IT Artifact
IT Examples
Layers
Microservices
Negative Code
Precomputation
Proof Of Work
Pseudorandom
Reusability
Scalability
Software Design
More ...
If you enjoyed this page, please consider bookmarking Simplicable.
 

Coding

A list of coding considerations and techniques.

Negative Code

An overview of negative code.

Synchronous

The definition of synchronous with examples.

Division By Zero

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

Null

An overview of null with examples.

Linux Processes

The basic types of linux process.

ls

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.

Computing

Examples of computing in everyday life.

Job Processing

A definition of job processing with examples.

System Architecture

Examples of system architecture diagrams.

Cloud-Scale

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.

Computer

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