Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Null pointer

Нулевой указатель

A pointer can hold a special value that means it points to nothing. Java has a similar concept, a `null` reference; calling an instance method through one throws a `NullPointerException` (NPE).

Also known as: null, NullPointerException, NPE

What a null pointer tells you

A pointer normally lets code access something in memory. A null pointer explicitly says there is nothing to access. That makes it useful as a marker: for example, the last node in a linked list can store a null next pointer to mark the end. It does not point to an object containing zero, and its meaning does not depend on how its bits are represented in memory.

Having null is different from using it

You can store a null pointer, pass it to a function, or compare it with the language's null pointer value. The problem starts when you try to access an object through it. In C and C++, reading or writing through a null pointer is undefined behavior. A crash is one possible outcome, not a guarantee. Java uses references rather than exposing raw pointers. Calling an instance method or accessing an instance field through a null reference throws NullPointerException, often shortened to NPE. The exception is a consequence of the access, not another name for null.

Example: finding a node in C++

Suppose findNode returns nullptr when no node has the requested ID. The caller must handle that result before reading a field:

const Node* node = findNode(id);
if (node == nullptr) {
    std::cout << "Node not found\n";
} else {
    std::cout << node->value << '\n';
}

Here, “not found” is a normal result, so the caller handles it explicitly. The null check does not prove that every non-null pointer is safe to use: the function's contract must also ensure that a returned node remains alive while the caller uses it.

Make absence part of the API contract

Decide whether a missing value is valid. A lookup might return Java's Optional<User> or Kotlin's User? to make that possibility visible in the return type. A method that finds multiple matches can usually return an empty list instead: the caller can iterate over it without a special branch. If a value is required, validate it where it enters the component. Silently substituting a default can hide a bad input or a broken assumption and make the eventual bug harder to trace.

How it comes up in interviews

In Java, you might be asked to debug order.getCustomer().getName(). The receiver of either call can be null: order, or the result of getCustomer(). If getName() merely returns null, that alone does not throw. Once you identify the receiver, explain whether its absence is expected and where to handle it. In C or C++, be ready to distinguish null from a dangling pointer: a dangling pointer may compare unequal to the null pointer value even though the object it once pointed to no longer exists.