0
trigger Member3ApprovalTrigger on Member__c (before insert) 
{
 Set<id> shippingNumbers = new Set<id>();

    for (Member__c collectNumFromOrder : Trigger.new) {
        shippingNumbers.add(collectNumFromOrder.Group_ID__c);
    }
     system.debug('Pranav2 '+shippingNumbers); 

    List<Group__c> contactList = [SELECT id, name,Group_Owner__c FROM Group__c WHERE id IN :shippingNumbers];
   system.debug('Pranav '+contactList); 

    Map<id, Group__c> shippingNumToContactMap = new Map<id, Group__c>();

    for (Group__c c : contactList) {
        shippingNumToContactMap.put(c.Group_Owner__c, c);
    }
    system.debug('Pranav3 '+shippingNumToContactMap);
    for (Member__c o : Trigger.new) {
         Group__c c = new Group__c();
        if (o.Group_ID__c!= null) {
            o.Member_Approver__c = shippingNumToContactMap.get(o.Group_id__c).id;

          system.debug('Pranav4 '+o.Member_Approver__c); 
        }
        else {
            o.Member_Approver__c = null;
        }
    }
}

User Pranav Bavadekar Date 8/11/2016 12:16:32 PM IST Status Attempt to de-reference a null object Application Browser Request Type Application Operation /a0A/e Duration (ms) 180 Log Size (bytes) 6,686 Log

37.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 12:16:32.0 (552412)|USER_INFO|[EXTERNAL]|00528000003hhTg|[email protected]|India Standard Time|GMT+05:30 12:16:32.0 (589352)|EXECUTION_STARTED 12:16:32.0 (592797)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS 12:16:32.0 (626576)|CODE_UNIT_STARTED|[EXTERNAL]|01q28000000aRWi|Member3ApprovalTrigger on Member trigger event BeforeInsert for [new] 12:16:32.0 (667639)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 12:16:32.0 (1293452)|HEAP_ALLOCATE|[72]|Bytes:3 12:16:32.0 (1338605)|HEAP_ALLOCATE|[77]|Bytes:152 12:16:32.0 (1361823)|HEAP_ALLOCATE|[342]|Bytes:408 12:16:32.0 (1392551)|HEAP_ALLOCATE|[355]|Bytes:408 12:16:32.0 (1423571)|HEAP_ALLOCATE|[467]|Bytes:48 12:16:32.0 (1464506)|HEAP_ALLOCATE|[139]|Bytes:6 12:16:32.0 (1498277)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:10 12:16:32.0 (1621780)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16 12:16:32.0 (1635338)|VARIABLE_SCOPE_BEGIN|[1]|this|Member3ApprovalTrigger|true|false 12:16:32.0 (1702860)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x6b2bbbdf 12:16:32.0 (1766838)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16 12:16:32.0 (1779619)|VARIABLE_SCOPE_BEGIN|[1]|this|Member3ApprovalTrigger|true|false 12:16:32.0 (1800952)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x6b2bbbdf 12:16:32.0 (1808763)|STATEMENT_EXECUTE|[1] 12:16:32.0 (1812497)|STATEMENT_EXECUTE|[3] 12:16:32.0 (1830511)|HEAP_ALLOCATE|[3]|Bytes:4 12:16:32.0 (1913011)|HEAP_ALLOCATE|[50]|Bytes:5 12:16:32.0 (1933303)|HEAP_ALLOCATE|[56]|Bytes:5 12:16:32.0 (1942866)|HEAP_ALLOCATE|[64]|Bytes:7 12:16:32.0 (2014366)|VARIABLE_ASSIGNMENT|[3]|this.shippingNumbers|{"s":1,"v":[]}|0x6b2bbbdf 12:16:32.0 (2209079)|HEAP_ALLOCATE|[5]|Bytes:5 12:16:32.0 (2232891)|HEAP_ALLOCATE|[5]|Bytes:4 12:16:32.0 (2241117)|VARIABLE_SCOPE_BEGIN|[5]|collectNumFromOrder|Member__c|true|false 12:16:32.0 (2528031)|VARIABLE_ASSIGNMENT|[5]|collectNumFromOrder|{"IsDeleted":false,"OwnerId":"00528000003hhTgAAI","Group_ID__c":"a092800000JAwBoAAL","First_Name__c":"HR7","Last_Name__c":"user"}|0x1be47a81 12:16:32.0 (2533836)|STATEMENT_EXECUTE|[5] 12:16:32.0 (2535414)|STATEMENT_EXECUTE|[6] 12:16:32.0 (2605178)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 12:16:32.0 (2621694)|HEAP_ALLOCATE|[5]|Bytes:5 12:16:32.0 (2634747)|VARIABLE_ASSIGNMENT|[5]|collectNumFromOrder|null| 12:16:32.0 (2638113)|STATEMENT_EXECUTE|[8] 12:16:32.0 (2641712)|HEAP_ALLOCATE|[8]|Bytes:8 12:16:32.0 (2701948)|HEAP_ALLOCATE|[8]|Bytes:20 12:16:32.0 (2723455)|HEAP_ALLOCATE|[8]|Bytes:28 12:16:32.0 (2743220)|USER_DEBUG|[8]|DEBUG|Pranav2 {a092800000JAwBoAAL} 12:16:32.0 (2749018)|STATEMENT_EXECUTE|[10] 12:16:32.0 (2752771)|HEAP_ALLOCATE|[10]|Bytes:65 12:16:32.0 (2761037)|HEAP_ALLOCATE|[10]|Bytes:4 12:16:32.0 (2769056)|HEAP_ALLOCATE|[10]|Bytes:7 12:16:32.0 (2814939)|HEAP_ALLOCATE|[10]|Bytes:8 12:16:32.0 (2823748)|HEAP_ALLOCATE|[10]|Bytes:8 12:16:32.0 (3057641)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|SELECT id, name, Group_Owner__c FROM Group__c WHERE id = :tmpVar1 12:16:32.0 (5890122)|SOQL_EXECUTE_END|[10]|Rows:1 12:16:32.0 (5912417)|HEAP_ALLOCATE|[10]|Bytes:8 12:16:32.0 (5928111)|HEAP_ALLOCATE|[10]|Bytes:79 12:16:32.0 (5957147)|HEAP_ALLOCATE|[10]|Bytes:8 12:16:32.0 (6009517)|VARIABLE_ASSIGNMENT|[10]|this.contactList|{"s":1,"v":[{"s":2,"v":{"Id":"a092800000JAwBoAAL","Name":"G-0001","Group_Owner__c":"00528000003hxQkAAI"}}]}|0x6b2bbbdf 12:16:32.0 (6015395)|STATEMENT_EXECUTE|[11] 12:16:32.0 (6020717)|HEAP_ALLOCATE|[11]|Bytes:7 12:16:32.0 (6110465)|HEAP_ALLOCATE|[11]|Bytes:82 12:16:32.0 (6128688)|HEAP_ALLOCATE|[11]|Bytes:89 12:16:32.0 (6141868)|USER_DEBUG|[11]|DEBUG|Pranav (Group__c:{Id=a092800000JAwBoAAL, Name=G-0001, Group_Owner__c=00528000003hxQkAAI}) 12:16:32.0 (6147673)|STATEMENT_EXECUTE|[13] 12:16:32.0 (6154042)|HEAP_ALLOCATE|[13]|Bytes:4 12:16:32.0 (6219232)|VARIABLE_ASSIGNMENT|[13]|this.shippingNumToContactMap|{"s":1,"v":{}}|0x6b2bbbdf 12:16:32.0 (6353290)|HEAP_ALLOCATE|[15]|Bytes:5 12:16:32.0 (6385711)|HEAP_ALLOCATE|[15]|Bytes:16 12:16:32.0 (6397761)|VARIABLE_SCOPE_BEGIN|[15]|c|Group__c|true|false 12:16:32.0 (6415290)|VARIABLE_ASSIGNMENT|[15]|c|{"s":1,"v":{"Id":"a092800000JAwBoAAL","Name":"G-0001","Group_Owner__c":"00528000003hxQkAAI"}}|0x6777bb87 12:16:32.0 (6419247)|STATEMENT_EXECUTE|[15] 12:16:32.0 (6420631)|STATEMENT_EXECUTE|[16] 12:16:32.0 (6483686)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:-4 12:16:32.0 (6498942)|HEAP_ALLOCATE|[15]|Bytes:5 12:16:32.0 (6511566)|VARIABLE_ASSIGNMENT|[15]|c|null| 12:16:32.0 (6514970)|STATEMENT_EXECUTE|[18] 12:16:32.0 (6518411)|HEAP_ALLOCATE|[18]|Bytes:8 12:16:32.0 (6584920)|HEAP_ALLOCATE|[18]|Bytes:101 12:16:32.0 (6601010)|HEAP_ALLOCATE|[18]|Bytes:109 12:16:32.0 (6611441)|USER_DEBUG|[18]|DEBUG|Pranav3 {00528000003hxQkAAI=Group__c:{Id=a092800000JAwBoAAL, Name=G-0001, Group_Owner__c=00528000003hxQkAAI}} 12:16:32.0 (6656210)|HEAP_ALLOCATE|[19]|Bytes:5 12:16:32.0 (6669821)|HEAP_ALLOCATE|[19]|Bytes:4 12:16:32.0 (6684829)|VARIABLE_SCOPE_BEGIN|[19]|o|Member__c|true|false 12:16:32.0 (6834650)|VARIABLE_ASSIGNMENT|[19]|o|{"IsDeleted":false,"OwnerId":"00528000003hhTgAAI","Group_ID__c":"a092800000JAwBoAAL","First_Name__c":"HR7","Last_Name__c":"user"}|0x1be47a81 12:16:32.0 (6839817)|STATEMENT_EXECUTE|[19] 12:16:32.0 (6841256)|STATEMENT_EXECUTE|[20] 12:16:32.0 (6853178)|HEAP_ALLOCATE|[20]|Bytes:4 12:16:32.0 (6947931)|VARIABLE_SCOPE_BEGIN|[20]|c|Group__c|true|false 12:16:32.0 (6959361)|VARIABLE_ASSIGNMENT|[20]|c|{}|0x555bae04 12:16:32.0 (6967510)|STATEMENT_EXECUTE|[21] 12:16:32.0 (6969019)|STATEMENT_EXECUTE|[22] 12:16:32.0 (7034056)|EXCEPTION_THROWN|[22]|System.NullPointerException: Attempt to de-reference a null object 12:16:32.0 (7195089)|HEAP_ALLOCATE|[22]|Bytes:41 12:16:32.0 (7268078)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.Member3ApprovalTrigger: line 22, column 1 12:16:32.0 (7282867)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.Member3ApprovalTrigger: line 22, column 1 12:16:32.7 (7326797)|CUMULATIVE_LIMIT_USAGE 12:16:32.7 (7326797)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 1 out of 100 Number of query rows: 1 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10

12:16:32.7 (7326797)|CUMULATIVE_LIMIT_USAGE_END

12:16:32.0 (9219747)|CODE_UNIT_FINISHED|Member3ApprovalTrigger on Member trigger event BeforeInsert for [new]

1
  • Please mention the exact issue? Aug 11, 2016 at 7:09

2 Answers 2

1
o.Member_Approver__c = shippingNumToContactMap.get(o.Group_id__c).id;

You should check if value is null in shippingNumToContactMap. It should look like this;

if(shippingNumToContactMap.get(o.Group_id__c) != null) o.Member_Approver__c = shippingNumToContactMap.get(o.Group_id__c).id;
else //error handling goes here
0
0

You ned to put a check here

if (o.Group_ID__c!= null && shippingNumToContactMap.containsKey(o.Group_id__c)) {
            o.Member_Approver__c = shippingNumToContactMap.get(o.Group_id__c).id;

because if map doesn't contain the key you will get an exception.

Update

Ad you want to assign the Group_Owner__c field then you should do this.

if (o.Group_ID__c!= null && shippingNumToContactMap.containsKey(o.Group_id__c)) {
  o.Member_Approver__c=shippingNumToContactMap.get(o.Group_id__c).Group_Owner__c  ;
4
  • Actually one more problem here is i want to assign Group_Owner__c value of Group__c object to Member_Approver__c field of Member__c object.But i am not able to accessing Group_Owner__c value here o.Member_Approver__c=shippingNumToContactMap.get(o.Group_id__c).id;
    – Pranav
    Aug 11, 2016 at 7:15
  • @Pranav check my updated answer. Aug 11, 2016 at 7:28
  • Thanks for answer @Tushar but it still shows Member_Approver__c field null.
    – Pranav
    Aug 11, 2016 at 9:46
  • @Pranav thats because if your map don't contain the value. check map Aug 11, 2016 at 10:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .