---
schema_version: "1"
id: 24de99d8df0b9ec1c0e3565de656bb8550a6c4cd98cf31f2194d69d860db7cb5
title: ApexNullPointerExceptionRule (Retired)
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/code-analyzer/salesforce-code-analyzer/guides/v3x/salesforce-graph-engine/apexnullpointerexception-rule.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/guide/apexnullpointerexception-rule.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 2c79907a7d1894a9083857492e0adf6b6ec8222d3c7134f1563b2dff0628b5de
products:
  - Salesforce Code Analyzer
---
# ApexNullPointerExceptionRule (Retired)

ApexNullPointerExceptionRule identifies Apex operations that dereference null objects and throw NullPointerExceptions. NullPointerExceptions generally indicate underlying problems in your code to address.

## Definition

- 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) {`.

## Interpreting Results

Match any violation message that you receive with this case to understand more about the violation.

### Common Case

> 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.

## Examples

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.

```
public void example1() {
	Object myStr = null;
	System.debug(myStr.toLowerCase());
// throws System.NullPointerException
// since method is invoked on null object.
}

public void example2(String myStr) {
	if (myStr == null) {
		// The following line throws System.NullPointerException, since the conditional confirms
		// that myStr is null when it reaches here.
		System.debug(myStr.toLowerCase());
	}
}

public void example3() {
	Integer i; // Not initialized
	Integer y = i + 2;
// throws System.NullPointerException since
// binary operation is invoked on null object
}
```

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.
