Java

Java

Made by DeepSource

Possible null access JAVA-E1083

Bug risk
Critical
cwe-476

This code contains a possible null pointer dereference. Double-check the code to ensure that the concerned variable always has a non-null value when accessed.

Bad Practice

In the example below, if someCondition is true, it is possible for value to be null when it reaches the assignment of valLen.

String value = "something";
if (someCondition) {
    value = null;
}

// ...

int valLen = value.length;  // Null pointer exception!

Recommended

Check for null before you use the value at the vulnerable point in code.

int valLen = value != null ? value.length : 0; 

If value was instead never intended to be null, consider changing the preceding logic to ensure that value is at least set to a safe default wherever possible:

if (someCondition) {
    value = "";
}

int valLen = value.length;  // No exception thrown.

References

  • CWE-476 - Null Pointer Dereference