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 aPageReferenceobject.public-scoped methods on Visualforce Controllers.global-scoped methods on any class.Messaging.InboundEmailResult handleInboundEmail()methods on implementations ofMessaging.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.
1public void example1() {
2 Object myStr = null;
3 System.debug(myStr.toLowerCase());
4// throws System.NullPointerException
5// since method is invoked on null object.
6}
7
8public void example2(String myStr) {
9 if (myStr == null) {
10 // The following line throws System.NullPointerException, since the conditional confirms
11 // that myStr is null when it reaches here.
12 System.debug(myStr.toLowerCase());
13 }
14}
15
16public void example3() {
17 Integer i; // Not initialized
18 Integer y = i + 2;
19// throws System.NullPointerException since
20// binary operation is invoked on null object
21}To fix NullPointerException issues, use one of these methods.
- Check that the object is
not-nullbefore 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.
Retired