ApexNullPointerExceptionRule (Generally Available)

ApexNullPointerExceptionRule identifies Apex operations that dereference null objects and throw NullPointerExceptions. NullPointerExceptions generally indicate underlying problems in your code to address.

Source

@AuraEnabled-annotated methods. @InvocableMethod-annotated methods. @NamespaceAccessible-annotated methods. @RemoteAction-annotated methods. Any method returning a PageReference object. public-scoped methods on Visualforce Controllers. global-scoped methods on any class. Messaging.InboundEmailResult handleInboundEmail() methods on implementations of Messaging.InboundEmailHandler. Any method targeted during invocation.

Sink

Any object dereference is a potential sink because object dereferences are the places where a null pointer exception can be thrown. Example: x.someMethod() |

Sanitizer

Non-null initialization of variables and null checks before accessing. Examples: String s = 'abcde'; , if (s != null) {.

Match any violation message that you receive with this case to understand more about the violation.

ApexNullPointerExceptionRule identifies Apex operations with a high likelihood of throwing a NullPointerException

The operation dereferences a null object and throws a NullPointerException. Review your code and add a null check.

In these examples, Apex Runtime throws a System.NullPointerException at the place where an operation is performed on a null object. The Graph Engine ApexNullPointerException rule preemptively identifies these operations.

To fix NullPointerException issues, use one of these methods.

  • Check that the object is not-null before performing the operation.
  • Make sure that all variables are initialized.

Avoid initializing variables to null. If your logic demands initialization to null, make sure to reassign a value to your variable before you invoke an operation on it.