a value indicating that a pointer does not refer to a valid object
In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value indicating that the pointer or reference does not refer to an object. Programs routinely use … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Null_pointer
Null pointer - Wikipedia
June 13, 2026 - In practice, dereferencing a null pointer may result in an attempted read or write from memory that is not mapped, triggering a segmentation fault or memory access violation. This may manifest itself as a program crash, or be transformed into a software exception that can be caught by program code.
Discussions

java - Null pointer access: The variable data can only be null at this location - Stack Overflow
You are initializing the array data to be null, when you try to access it it gives you null pointer access error. More on stackoverflow.com
🌐 stackoverflow.com
Null Pointer Access Error

What are you doing when this happens? This sounds like a fringe programming error.

More on reddit.com
🌐 r/7kglobal
6
1
October 19, 2016
java - What is a NullPointerException, and how do I fix it? - Stack Overflow
What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them? What methods/tools can be used to determine the cause so that you stop the exception from causing the progra... More on stackoverflow.com
🌐 stackoverflow.com
NULL Pointer dereference does not cause a hard fault.
I was under the assumption that dereferncing a NULL pointer or trying to write something to the NULL address would cause a hard fault on any ARM architecture microcontroller. Obviously I was wrong. More on devzone.nordicsemi.com
🌐 devzone.nordicsemi.com
1
0
March 27, 2020
🌐
Quora
quora.com › What-happens-when-we-try-to-access-a-null-pointer-in-C
What happens when we try to access a null pointer in C? - Quora
Answer (1 of 4): The standard says that accessing a NULL ptr is “undefined behavior”. Undefined behavior can be anything, including: * Nothing at all - continue running the program as if nothing happened * Crashing the application * Corrupting application data From Wikipedia we have this: ...
🌐
Computerblog
computerblog.org › home › how do i fix the null pointer assignment error?
How do I fix the null pointer assignment error? - Tommy's Computer Blog
February 11, 2022 - Running a program that contains a reference to the NULL flag immediately raises a segmentation fault. I think this is not just a primary null pointer, each of these pointers are wild. If you try to access many key areas, you will run into null pointer assignment errors.
Top answer
1 of 12
4227

There are two overarching types of variables in Java:

  1. Primitives: variables that contain data. If you want to manipulate the data in a primitive variable you can manipulate that variable directly. By convention primitive types start with a lowercase letter. For example variables of type int or char are primitives.

  2. References: variables that contain the memory address of an Object i.e. variables that refer to an Object. If you want to manipulate the Object that a reference variable refers to you must dereference it. Dereferencing usually entails using . to access a method or field, or using [ to index an array. By convention reference types are usually denoted with a type that starts in uppercase. For example variables of type Object are references.

Consider the following code where you declare a variable of primitive type int and don't initialize it:

int x;
int y = x + x;

These two lines will crash the program because no value is specified for x and we are trying to use x's value to specify y. All primitives have to be initialized to a usable value before they are manipulated.

Now here is where things get interesting. Reference variables can be set to null which means "I am referencing nothing". You can get a null value in a reference variable if you explicitly set it that way, or a reference variable is uninitialized and the compiler does not catch it (Java will automatically set the variable to null).

If a reference variable is set to null either explicitly by you or through Java automatically, and you attempt to dereference it you get a NullPointerException.

The NullPointerException (NPE) typically occurs when you declare a variable but did not create an object and assign it to the variable before trying to use the contents of the variable. So you have a reference to something that does not actually exist.

Take the following code:

Integer num;
num = new Integer(10);

The first line declares a variable named num, but it does not actually contain a reference value yet. Since you have not yet said what to point to, Java sets it to null.

In the second line, the new keyword is used to instantiate (or create) an object of type Integer, and the reference variable num is assigned to that Integer object.

If you attempt to dereference num before creating the object you get a NullPointerException. In the most trivial cases, the compiler will catch the problem and let you know that "num may not have been initialized," but sometimes you may write code that does not directly create the object.

For instance, you may have a method as follows:

public void doSomething(SomeObject obj) {
   // Do something to obj, assumes obj is not null
   obj.myMethod();
}

In which case, you are not creating the object obj, but rather assuming that it was created before the doSomething() method was called. Note, it is possible to call the method like this:

doSomething(null);

In which case, obj is null, and the statement obj.myMethod() will throw a NullPointerException.

If the method is intended to do something to the passed-in object as the above method does, it is appropriate to throw the NullPointerException because it's a programmer error and the programmer will need that information for debugging purposes.

In addition to NullPointerExceptions thrown as a result of the method's logic, you can also check the method arguments for null values and throw NPEs explicitly by adding something like the following near the beginning of a method:

// Throws an NPE with a custom error message if obj is null
Objects.requireNonNull(obj, "obj must not be null");

Note that it's helpful to say in your error message clearly which object cannot be null. The advantage of validating this is that 1) you can return your own clearer error messages and 2) for the rest of the method you know that unless obj is reassigned, it is not null and can be dereferenced safely.

Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameter and behave differently. You should also explain this in the documentation. For example, doSomething() could be written as:

/**
  * @param obj An optional foo for ____. May be null, in which case
  *  the result will be ____.
  */
public void doSomething(SomeObject obj) {
    if(obj == null) {
       // Do something
    } else {
       // Do something else
    }
}

Finally, How to pinpoint the exception & cause using Stack Trace

What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?

Sonar with find bugs can detect NPE. Can sonar catch null pointer exceptions caused by JVM Dynamically

Now Java 14 has added a new language feature to show the root cause of NullPointerException. This language feature has been part of SAP commercial JVM since 2006.

In Java 14, the following is a sample NullPointerException Exception message:

in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "list" is null

List of situations that cause a NullPointerException to occur

Here are all the situations in which a NullPointerException occurs, that are directly* mentioned by the Java Language Specification:

  • Accessing (i.e. getting or setting) an instance field of a null reference. (static fields don't count!)
  • Calling an instance method of a null reference. (static methods don't count!)
  • throw null;
  • Accessing elements of a null array.
  • Synchronising on null - synchronized (someNullReference) { ... }
  • Any integer/floating point operator can throw a NullPointerException if one of its operands is a boxed null reference
  • An unboxing conversion throws a NullPointerException if the boxed value is null.
  • Calling super on a null reference throws a NullPointerException. If you are confused, this is talking about qualified superclass constructor invocations:
class Outer {
    class Inner {}
}
class ChildOfInner extends Outer.Inner {
    ChildOfInner(Outer o) { 
        o.super(); // if o is null, NPE gets thrown
    }
}
  • Using a for (element : iterable) loop to loop through a null collection/array.

  • switch (foo) { ... } (whether its an expression or statement) can throw a NullPointerException when foo is null.

  • foo.new SomeInnerClass() throws a NullPointerException when foo is null.

  • Method references of the form name1::name2 or primaryExpression::name throws a NullPointerException when evaluated when name1 or primaryExpression evaluates to null.

    a note from the JLS here says that, someInstance.someStaticMethod() doesn't throw an NPE, because someStaticMethod is static, but someInstance::someStaticMethod still throw an NPE!

* Note that the JLS probably also says a lot about NPEs indirectly.

2 of 12
972

NullPointerExceptions are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NullPointerException. These are the most common, but other ways are listed on the NullPointerException javadoc page.

Probably the quickest example code I could come up with to illustrate a NullPointerException would be:

public class Example {

    public static void main(String[] args) {
        Object obj = null;
        obj.hashCode();
    }

}

On the first line inside main, I'm explicitly setting the Object reference obj equal to null. This means I have a reference, but it isn't pointing to any object. After that, I try to treat the reference as though it points to an object by calling a method on it. This results in a NullPointerException because there is no code to execute in the location that the reference is pointing.

(This is a technicality, but I think it bears mentioning: A reference that points to null isn't the same as a C pointer that points to an invalid memory location. A null pointer is literally not pointing anywhere, which is subtly different than pointing to a location that happens to be invalid.)

🌐
ScienceDirect
sciencedirect.com › topics › computer-science › null-pointer
Null Pointer - an overview | ScienceDirect Topics
In layman’s terms, a null pointer is a pointer to an address in the memory space that does not have a meaningful value and cannot be referenced by the calling program, for whatever reason. This will normally lead to an unhandled error, resulting in a segmentation fault.
Find elsewhere
🌐
Udemy
blog.udemy.com › home › it & development › software development › java null pointer exception basics for beginners
Java Null Pointer Exception Basics for Beginners - Udemy Blog
April 14, 2026 - As mentioned in JAVA’s API documents, Null Pointer Exception is thrown when the null value is used instead of using a reference value. This essentially means that a part of code is trying to access a reference without a value.
🌐
Dronatechnoworld
dronatechnoworld.com › 2023 › 01 › what-is-null-pointer-error-in-java-and.html
Technology World: What is Null Pointer Error in Java and How to fix it?
January 24, 2023 - In summary, a null pointer error occurs when a program tries to access an object or variable that has a null value. The error can be fixed by checking for null values before trying to access an object and handling the error properly.
Top answer
1 of 5
4
The C++ Standard doesn't care. From Section 3.27: Undefined behavior may be expected when this document omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. The dereferencing operators * and -> are only defined for the first type of pointer in the standard - a pointer to an object or function. Everything else is undefined, and is up to how the compiler and runtime environment are implemented. That way lies dragons. On my system: Linux doesn't care, either. Either the process is allowed to access a particular address in memory, or it isn't. If I dereference the null pointer, I get a segfault, just like if I try to dereference a pointer containing 0xdeadbeef. Null isn't special other than that the OS will never allow you to access memory with that address and that it's frequently used as a pointer value that signifies nonexistence.
2 of 5
4
The difference is that NullPointerExceptions come from managed languages which actually check if you're dereferencing NULL, whereas a segfault is the OS telling you to go away and stop messing with memory you shouldn't be messing with. It's really a matter of how much abstraction there is between the language and the underlying architecture (i.e. how low/high level the language is). In a language like Java you can basically imagine every method call/field access looks like this C++ code (and it shares the associated performance cost!): if (!obj) throw NullPointerException(blahblah); obj->method();
🌐
STMicroelectronics Community
community.st.com › t5 › stm32-mcus-products › access-null-pointer-m4-vs-m33 › td-p › 141062
Access Null pointer - M4 vs M33 - STMicroelectronics Community
October 7, 2022 - Hello, Code that was compiled for stm32F407 ( core M4) has an bug that accesses null pointer. The assembly code is as follows: STRB r1, [r0, #0x01] Where r0=0 and r1=0. Although MCU accesses null pointer, it does not enter hard-fault. But, the same code on stm32U585 (core M33, w/o trust zone), ...
🌐
Medium
medium.com › @kiranbjm › what-is-a-null-pointer-exception-de351fb907e7
What is a Null Pointer Exception? | by Kiran Benny Joseph | Medium
October 26, 2024 - A NullPointerException happens when your code tries to access properties, methods, or functions on a variable that hasn’t been assigned a…
🌐
White Knight Labs
whiteknightlabs.com › 2025 › 06 › 24 › understanding-null-pointer-dereference-in-windows-kernel-drivers
Understanding Null Pointer Dereference in Windows Kernel Drivers | White Knight Labs
June 24, 2025 - A null pointer dereference happens when a driver tries to access memory through a pointer that hasn’t been properly initialized—usually pointing to address 0x0. In user mode, this might just crash an app, but in kernel mode, it’s a lot more serious. Since the kernel operates with full system privileges with limited error handling, dereferencing a null pointer can trigger a blue screen of death (BSOD) and bring down the entire system.
🌐
Quora
quora.com › Everyone-says-dereferencing-a-null-pointer-is-really-bad-but-what-will-actually-happen-and-why-Im-especially-interested-in-systems-without-paging-as-a-page-fault-would-probably-occour-if-paging-is-present
Everyone says dereferencing a null pointer is really bad, but what will actually happen, and why? (I'm especially interested in systems without paging, as a page fault would probably occour if paging is present) - Quora
Hardware detects an illegal memory access to address 0 and raises a fault ( ... Dereferencing a null pointer means using an address value of zero (or whatever the language/runtime defines as null) as if it were a valid pointer to memory. What actually happens depends on hardware, OS, language ...