NullPointerException – Data Manipulation Language (DML) – Salesforce Certified Platform Developer I Study Guide

5.5 NullPointerException

In Apex, a NullPointerException occurs when you attempt to access or manipulate an object that is null. In other words, you are trying to perform an operation on an object reference that doesn’t point to any instance of an object. This can lead to runtime errors and unexpected behavior in your Apex code.

To address this issue the Safe Navigation Operator in Apex, denoted by (?.), is a useful feature that allows you to safely access properties and methods of an object that may be null, without throwing a NullPointerException. It is used to perform null checks before accessing object properties or invoking methods, ensuring that the code doesn’t break if the object is null.

The Safe Navigation Operator works by checking if the object is null before accessing its property or invoking a method. If the object is null, the operator returns null instead of throwing a NullPointerException. If the object is not null, the property or method is accessed normally.

Here’s a simple example to illustrate the use of the Safe Navigation Operator:

Step 1: Create the class MyClass and save public class MyClass {

public String getName() {

return ‘John’;

}

}

Step 2: Open the anonymous window

  • Without Safe Navigation Operator MyClass myObject = null;

String name = myObject.getName();

System.debug(‘Name: ‘ + name);

  • This would throw a NullPointerException
  • With Safe Navigation Operator

MyClass myObject = null;

String name = myObject?.getName();

System.debug(‘Name: ‘ + name); // This will output: null

In the code above, if you uncomment the line without the Safe Navigation Operator and run it with myObject being null, it would throw a NullPointerException. However, by using the Safe Navigation Operator, the code with myObject?.getName() will not throw an exception; instead, it assigns null to the name variable.

This concept is generally related to methods in Apex, but it can also be applied to properties and collections to avoid NullPointerException when accessing or manipulating them.

CHAPTER 6 SOQL and SOSL

6.1  Salesforce Object Query Language (SOQL)

Salesforce Object Query Language (SOQL) is a query language used to retrieve data from Salesforce databases. It is similar to SQL (Structured Query Language) but has some syntax differences and is designed specifically for querying Salesforce objects.

SOQL allows developers to query data from a single object or from multiple related objects in the database. It is commonly used to retrieve specific records or to search for records that meet certain criteria. With SOQL you can specify the fields you want to retrieve, filter the records based on conditions, and sort the results.

The basic structure of a Salesforce Object Query Language (SOQL) query follows a specific format, and here is the typical structure:

Step 1: Open the Salesforce Developer Console.

Step 2: In the Developer Console, click on the “Query Editor” tab.

SELECT Id, Name, Owner.Name

FROM Account

WHERE Name LIKE ‘MyAccount’

ORDER BY Owner.Name

Step 3: In the Query Editor, enter your SOQL query. For your example, you can copy and paste the code above. You can change to another name within your existing accounts.