function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cARL scARL s 

Unable to dereference a null object

I am new to salesforce but have lots of experience in java programming.  I have a need to dereference a null object in an apex class, but I am unable to do so.  Please advise me on the proper way to dereference a null object in apex.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

As several people here have already stated, you can not deference a null object. The best that you can do is to use a try-catch block:

 

string s;
try {
  s = a.accountname.substring(0,10);
} catch(exception e) {
 s = '';
}

Although this is less efficient than simply using if-then statements (as mentioned earlier). 

 

If your programming lead can not understand this, then they should be forced to take the DEV classes offered by salesforce.com, because they clearly do not grasp the concept of a null object. A null object has no assigned memory, so it can not be dereferenced, because there is nothing to dereference to. A reference to a memory address is known as a pointer. To dereference a pointer is to follow that pointer to the memory address that the pointer references. If there is no memory to reference, how can you possibly access it?

 

Just as it is impossible to send mail to a mailing address that does not exist, it is no more likely that you will be able to dereference a memory location that does not exist. If you go into a building with 5 floors, you can not take an elevator or the stairs to the 20th floor, no matter how you try. This is a fundamental mechanic of programming logic, which is firmly rooted in real-world logic. A null object does not have the value "zero" (or 0, if you will), it has no value at all.

 

Here's another code example that illustrates the same problem: 

 

integer a, b;
a = b + 5;

What is the value of "a" after executing this code? We do not know the value of "b" (because it has no value at all), and so therefore the statement becomes "add 5 to an unknown value", which does not result in a legal integer that could possibly be assigned to "a".

 

There are a few languages that initialize all objects for you (such as .NET), and those languages will happily let you deference any object, because it is not truly null (it has a default value for you). PHP offers an auto-initializer function that lets you generate elements on the fly. However, Apex Code does not offer either of these abilities. There is no automatic instantiation in Apex Code. If an object is uninitialized, it is null, has no memory assigned to it, and can not be used as a real object, and nothing you do will change that fact.

 

Tell your programming lead that they should first write a working example for you to use, because you are unable to find this information on your own despite asking many veteran programmers and reading through the developer documentation. They will not be able to produce a working example, or they will show you one of the methods that we have already outlined in this post.

All Answers

sfdcfoxsfdcfox

It is the same as it is in Java. You always compare to the (case-insensitive) global static final Object: null.

 

A brief foray with nulls:

 

Account acc;

if(acc == null) { 
    acc = new Account();
}

if(acc != null) {
    if(acc.name == null) {
        acc.name = 'Foo';
    }
}

acc = null;

There's no special tricks found in Apex Code that are not in Java. Even the NullPointerException can be caught and handled just like you would perform in Java.

cARL scARL s

I'm sorry, but you misunderstood. I don't need to detect the null; I need to dereference it. For example:

Account acc1;
Account acc2 = new Account();
acc2.name = acc1.name.substring(0,10);
Insert new Account[]{acc1,acc2};


I always get an error on the third line. How can I deference that null without an error?



Rahul SharmaRahul Sharma

You are getting that error because you are trying to fetch Name From Null object.

 

Account acc2 = new Account(); // Initialized, contains no value (Null)

acc2.name = acc1.name.substring(0,10); // You are trying to fetch Name from Null Object

 

 

sfdcfoxsfdcfox

Rahul is correct. Allow me elaborate a bit further.

 

It is an error to attempt to dereference a null object. When you create an SObject in memory, only fields initialized in the constructor or that are queried will have non-null values in memory.

 

As you know, you can not do this in Java:

 

String a, b;
b = a.substring(0,3);

This is an extension of that logic. When you call an Sobject constructor, all of its members are initially a null value. You have to first assign a value or query a value into memory before you can use the member as a proper object.

cARL scARL s

Yes, that you for your help, but I realize this already.  However, our programming lead insists that this must be done in this manner.  Is there any way?

sfdcfoxsfdcfox

As several people here have already stated, you can not deference a null object. The best that you can do is to use a try-catch block:

 

string s;
try {
  s = a.accountname.substring(0,10);
} catch(exception e) {
 s = '';
}

Although this is less efficient than simply using if-then statements (as mentioned earlier). 

 

If your programming lead can not understand this, then they should be forced to take the DEV classes offered by salesforce.com, because they clearly do not grasp the concept of a null object. A null object has no assigned memory, so it can not be dereferenced, because there is nothing to dereference to. A reference to a memory address is known as a pointer. To dereference a pointer is to follow that pointer to the memory address that the pointer references. If there is no memory to reference, how can you possibly access it?

 

Just as it is impossible to send mail to a mailing address that does not exist, it is no more likely that you will be able to dereference a memory location that does not exist. If you go into a building with 5 floors, you can not take an elevator or the stairs to the 20th floor, no matter how you try. This is a fundamental mechanic of programming logic, which is firmly rooted in real-world logic. A null object does not have the value "zero" (or 0, if you will), it has no value at all.

 

Here's another code example that illustrates the same problem: 

 

integer a, b;
a = b + 5;

What is the value of "a" after executing this code? We do not know the value of "b" (because it has no value at all), and so therefore the statement becomes "add 5 to an unknown value", which does not result in a legal integer that could possibly be assigned to "a".

 

There are a few languages that initialize all objects for you (such as .NET), and those languages will happily let you deference any object, because it is not truly null (it has a default value for you). PHP offers an auto-initializer function that lets you generate elements on the fly. However, Apex Code does not offer either of these abilities. There is no automatic instantiation in Apex Code. If an object is uninitialized, it is null, has no memory assigned to it, and can not be used as a real object, and nothing you do will change that fact.

 

Tell your programming lead that they should first write a working example for you to use, because you are unable to find this information on your own despite asking many veteran programmers and reading through the developer documentation. They will not be able to produce a working example, or they will show you one of the methods that we have already outlined in this post.

This was selected as the best answer
Rahul SharmaRahul Sharma

What a nice explanation and with example.

Thanks sdfcfox

cARL scARL s

My programming lead had not heard of a Try-Catch block before so I explained it and he is willing to let me use that.

 

Thanks for the idea!