Missing Check against Null

Learn how to prevent null pointer exceptions with real code examples and best practices. Protect your application from crashes and unexpected behavior.

Tools recognizing this:

Opengrep Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is Missing Check against Null and How Does it Work?

Missing Check against Null is a common programming vulnerability that occurs when code fails to verify if an object reference is null before using it. This can lead to null pointer exceptions (NPEs) at runtime, causing application crashes and potential security vulnerabilities.

The absence of null checks can result in:

  • Application crashes and unexpected termination

  • System instability and poor user experience

  • Potential security vulnerabilities through error messages

  • Resource leaks and memory issues

This guide covers Missing Check against Null issues, examples, prevention methods, and how to properly handle null references using real-world techniques.

One Simple Missing Check against Null Example

Consider this classic example of accessing an object without null checking:

User user = getUserFromDatabase();
String username = user.getUsername();  // Could throw NullPointerException if user is null

If getUserFromDatabase() returns null, this code will throw a NullPointerException when trying to access getUsername().

The safer version would be:

User user = getUserFromDatabase(); String username = user != null ? user.getUsername() : "default";

This ensures the code handles the null case gracefully instead of throwing an exception.

Null Check Prevention Methods: How to Fix Your Code

The most efficient way to fix Missing Check against Null issues is by using proper null checking mechanisms and modern language features designed for null safety.

These include using the Optional pattern, null-safe operators, Objects.requireNonNull(), and explicit null checks where appropriate.

Code Samples

Vulnerable Code

public void processUser(User user) {
    String name = ;
    System.out.println("Processing user: " + name);
}

Fixed Code

public void processUser(User user) {
    Objects.requireNonNull(user, "User cannot be null");
    String name = user.getName();
    System.out.println("Processing user: " + name);
}

Fix Explanation

The vulnerable code assumes user is never null.The fix uses Objects.requireNonNull() to validate the parameter.This provides a clear error message if null is passed.The check happens early, preventing deeper issues in the method.

Need more help in preventing Missing Check against Null?

Mobb supports fixing many forms of Missing Check against Null vulnerabilities, and can mitigate your issues in batch.

Start now for free at app.mobb.ai

We'd love your feedback!

We're excited to hear your thoughts and ideas about fixing vulnerabilities.

Book a meeting or Contact us if you have any corrections, questions or suggestions. Start now for free at https://app.mobb.ai

Last updated

Was this helpful?