JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A null pointer dereference, including the following.
Corresponding deep warning class: JAVA.DEEPNULL.DEREF.
If null is dereferenced, Java runs into a NullPointerException. For this reason, programmers must ensure that the content of expressions dereferenced in their programs is never null.
CodeSonar provides two sets of warning classes covering various aspects of null value handling.
For the most comprehensive null pointer checking, enable both sets of warning classes: JAVA.NULL.* and JAVA.DEEPNULL.*
When JAVA_ANALYSIS_STRICT_MODE=No, warnings of this class will not be issued if there are indications that the possibility of a NullPointerException has been recognized and accounted for. For example, warnings will not be issued for code inside a try-catch block that explicitly catches NullPointerException, or for a JUnit test that is annotated as expecting this exception.
When JAVA_ANALYSIS_STRICT_MODE=Yes, warnings will be issued even in these cases.
| Class Name | Null Pointer Dereference (Java) | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||||||||
| Mnemonic | JAVA.NULL.DEREF | ||||||||||||
| Categories |
|
||||||||||||
| Availability | Available for Java and Kotlin. |
||||||||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Null Pointer Dereference (Java)" |
// ArrayProgram.java public class ArrayProgram { public static void Length() { int[] arr1 = null; int arrayLength = arr1.length; // "Null Pointer Dereference (Java)" warning issued here" } public static void Load() { int[] arr2 = null; int n = arr2[0]; // "Null Pointer Dereference (Java)" warning issued here" } public static void Store() { int[] arr3 = null; arr3[0] = 3; // "Null Pointer Dereference (Java)" warning issued here" } }
// CountingStars.java public class CountingStars { int stars; Object lock; public void add(int value){ synchronized(lock){ // "Null Pointer Dereference (Java)" warning issued here this.stars += value; } } }
To avoid the NullPointerException caused by attempting to synchronize on null, initialize the lock object as follows.
// CountingStars.java, after modification
public class CountingStars {
int stars;
final Object lock = new Object();
public void add(int value){
synchronized(lock){
this.stars += value;
}
}
}
// MyExceptionHandler.java public class MyExceptionHandler { public void handle(Exception e) throws Exception { if(e != null ) { System.out.println("Exception found: " + e.getClass()); } throw e; // "Null Pointer Dereference (Java)" warning issued here } }
// MyMainClass.java public class MyMainClass { public static void main(String[] args) { Place p = null; p.h = "These's no place like home!"; // "Null Pointer Dereference (Java)" warning issued here } private static class Place { public String h; // ... } }
// TestGetFieldFromNullWarning.java package test; public class TestGetFieldFromNullWarning { public boolean condition; public class Bean { private String field = "value"; public String getField() { return field; } public void setField(String field) { this.field = field; } } public void getFieldFromNullWarning(){ Bean bean = null; if(condition) bean = new Bean(); if(bean.getField().equals("")) { // "Null Pointer Dereference (Java)" warning issued here System.out.println("Field is empty"); } } }
Highlighting variables which value is always null may help discovering dead code or simply logic that is no more required.
// TestVariableCanOnlyBeNullWarning.java package test; public class TestVariableCanOnlyBeNullWarning { @SuppressWarnings("unused") public boolean variableCanOnlyBeNullWarning(){ int[] var = null; if (var == null) // "Null Pointer Dereference (Java)" warning issued here return false; for (int i = 0; i < var.length; i++) System.out.print("pippo"); return true; } }
Check if the warning corresponds to a situation where null might actually be dereferenced at runtime. If that is the case, add a nullness check for the value being dereferenced, or change the logic of the code. Sometimes, a warning of this checker corresponds to a spurious nullness check, that can be removed.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.