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
Sami CohenSami Cohen 

List Problem

Hi all,
Recently, we encoutered new problem in List iteration.
The following code generates an exception saying that  the element "d" is null :
 
Code:
for(Opportunity d : [select name from opportunity])
{
    // do something with d
}

We resolved the bug by inserting a "If" condition before performing any operation into the loop.
Is is very strong!!!!
Any comment?

hisrinuhisrinu
Yes, you need to check, because if there are no records then d will be null.
So you are trying to access a null object then it will show an error.
JimRaeJimRae
This could happen if the user you run the code as does not have access to any Opportunities, or if you try to access any other field in the "d" element besides "name".