+ Start a Discussion
bouscalbouscal 

Why does Set.add(c.id) fail?

I am trying to understand what I am doing wrong here.  I have defined a Set that will hold IDs then used the .add method to add the ID from a Case but it fails.  I've redefined the Set as a set of strings as well but either way I get a null pointer exception even though there is always at least 1 record in the list.

Here's a snippet of my class
Set<id> RecIds; 
for(Case c:cs){
            System.debug('***** entering loop c:cs cs.size() = ' + cs.size() + ' *****' );
            if(c.RecordTypeId==rsc){
                System.debug('***** inside rsc IF statement c.id = ' + c.id + ' ***** ');
            	RecIds.add(c.id);
                System.debug('***** c.id ' + c.id + ' was added to RecIds *****');
            }
and here's the debug log
 
15:04:01.036 (15036876514)|SYSTEM_METHOD_ENTRY|[24]|System.debug(ANY)
15:04:01.036 (15036881962)|USER_DEBUG|[24]|DEBUG|***** entering loop c:cs cs.size() = 1 *****
15:04:01.036 (15036886758)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
15:04:01.036 (15036944939)|SYSTEM_METHOD_ENTRY|[25]|Id.compareTo(Id, Id, Boolean)
15:04:01.036 (15036972914)|SYSTEM_METHOD_EXIT|[25]|Id.compareTo(Id, Id, Boolean)
15:04:01.036 (15036998417)|SYSTEM_METHOD_ENTRY|[26]|String.valueOf(Object)
15:04:01.037 (15037022793)|SYSTEM_METHOD_EXIT|[26]|String.valueOf(Object)
15:04:01.037 (15037039033)|SYSTEM_METHOD_ENTRY|[26]|System.debug(ANY)
15:04:01.037 (15037044727)|USER_DEBUG|[26]|DEBUG|***** inside rsc IF statement c.id = 500S0000007LWuXIAW ***** <=== c.id has a valid value
15:04:01.037 (15037049435)|SYSTEM_METHOD_EXIT|[26]|System.debug(ANY)
15:04:01.037 (15037108600)|METHOD_EXIT|[15]|01pS000000070OI|HOL_NA_CaseEscalated.processCase(LIST<Case>)  <=== trying to add it to the Set fails
15:04:01.037 (15037198686)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.HOL_NA_CaseEscalated.processCase: line 27, column 1
Trigger.Cases: line 15, column 1
15:04:01.037 (15037221135)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object


 
Best Answer chosen by bouscal
Tyler Mowbrey 1Tyler Mowbrey 1
Hello,

You need to initialize your set. The error is saying that you are de-referencing a null object, meaning the set is null.

On line one do the following: Set<id> RecIds = new Set<id>();

Tyler

All Answers

Tyler Mowbrey 1Tyler Mowbrey 1
Hello,

You need to initialize your set. The error is saying that you are de-referencing a null object, meaning the set is null.

On line one do the following: Set<id> RecIds = new Set<id>();

Tyler
This was selected as the best answer
bouscalbouscal
Thanks Tyler, been learning too many languages at once.  Helps me get a clearer grasp on how each language does things but often tends to let me confuse the syntax.  :-/