|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object on my Apex Trigger











up vote
2
down vote

favorite












I'm kind of new in Apex development and I'm writing a before insert, before update trigger to a custom object that is going to be upserted through an API integration.



Is very basic code but I don't see what I'm doing wrong, I need an extra couple of eyes.



Here the code:



    trigger ROIDUpdate on RO_Item__c (before insert, before update) {


//Setting the mapping

Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();

for (RO_Item__c ROI :Trigger.New){

system.debug('<---New Repair Order Item--->');

if (ROI.SOD_AUTO_KEY__c!=Null){


m.put(ROI.SOD_AUTO_KEY__c, ROI);
system.debug('Map key:'+ m.get(ROI.SOD_AUTO_KEY__c) );


}


}

//Looking for the Sales Order Item that matches the Repair Order SOD Auto Key
List<SO_Item__c> soilst = [SELECT SOId__c, SOD_AUTO_KEY__c
FROM SO_Item__c
WHERE SOD_AUTO_KEY__c in :m.keySet() ];

//Write the ID of the Sales Order into the Repair Order. Field SO_ID__c under RO_Item__c
for(SO_Item__c b :soilst){

if (b.SOD_AUTO_KEY__c!=Null){

m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
system.debug('SO ID to be inserted:'+ b.SOId__c );

}


}

}


I've tried debugging it and I'm getting the NullPointerException on the line containing: m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;



What am I doing wrong?



Thanks in advance.










share|improve this question







New contributor




Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Are you sure that the values of b.SOD_AUTO_KEY__c and ROI.SOD_AUTO_KEY__c will be always same? Are you getting exception in this line -- m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;?
    – Jayant Das
    1 hour ago

















up vote
2
down vote

favorite












I'm kind of new in Apex development and I'm writing a before insert, before update trigger to a custom object that is going to be upserted through an API integration.



Is very basic code but I don't see what I'm doing wrong, I need an extra couple of eyes.



Here the code:



    trigger ROIDUpdate on RO_Item__c (before insert, before update) {


//Setting the mapping

Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();

for (RO_Item__c ROI :Trigger.New){

system.debug('<---New Repair Order Item--->');

if (ROI.SOD_AUTO_KEY__c!=Null){


m.put(ROI.SOD_AUTO_KEY__c, ROI);
system.debug('Map key:'+ m.get(ROI.SOD_AUTO_KEY__c) );


}


}

//Looking for the Sales Order Item that matches the Repair Order SOD Auto Key
List<SO_Item__c> soilst = [SELECT SOId__c, SOD_AUTO_KEY__c
FROM SO_Item__c
WHERE SOD_AUTO_KEY__c in :m.keySet() ];

//Write the ID of the Sales Order into the Repair Order. Field SO_ID__c under RO_Item__c
for(SO_Item__c b :soilst){

if (b.SOD_AUTO_KEY__c!=Null){

m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
system.debug('SO ID to be inserted:'+ b.SOId__c );

}


}

}


I've tried debugging it and I'm getting the NullPointerException on the line containing: m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;



What am I doing wrong?



Thanks in advance.










share|improve this question







New contributor




Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Are you sure that the values of b.SOD_AUTO_KEY__c and ROI.SOD_AUTO_KEY__c will be always same? Are you getting exception in this line -- m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;?
    – Jayant Das
    1 hour ago















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm kind of new in Apex development and I'm writing a before insert, before update trigger to a custom object that is going to be upserted through an API integration.



Is very basic code but I don't see what I'm doing wrong, I need an extra couple of eyes.



Here the code:



    trigger ROIDUpdate on RO_Item__c (before insert, before update) {


//Setting the mapping

Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();

for (RO_Item__c ROI :Trigger.New){

system.debug('<---New Repair Order Item--->');

if (ROI.SOD_AUTO_KEY__c!=Null){


m.put(ROI.SOD_AUTO_KEY__c, ROI);
system.debug('Map key:'+ m.get(ROI.SOD_AUTO_KEY__c) );


}


}

//Looking for the Sales Order Item that matches the Repair Order SOD Auto Key
List<SO_Item__c> soilst = [SELECT SOId__c, SOD_AUTO_KEY__c
FROM SO_Item__c
WHERE SOD_AUTO_KEY__c in :m.keySet() ];

//Write the ID of the Sales Order into the Repair Order. Field SO_ID__c under RO_Item__c
for(SO_Item__c b :soilst){

if (b.SOD_AUTO_KEY__c!=Null){

m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
system.debug('SO ID to be inserted:'+ b.SOId__c );

}


}

}


I've tried debugging it and I'm getting the NullPointerException on the line containing: m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;



What am I doing wrong?



Thanks in advance.










share|improve this question







New contributor




Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm kind of new in Apex development and I'm writing a before insert, before update trigger to a custom object that is going to be upserted through an API integration.



Is very basic code but I don't see what I'm doing wrong, I need an extra couple of eyes.



Here the code:



    trigger ROIDUpdate on RO_Item__c (before insert, before update) {


//Setting the mapping

Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();

for (RO_Item__c ROI :Trigger.New){

system.debug('<---New Repair Order Item--->');

if (ROI.SOD_AUTO_KEY__c!=Null){


m.put(ROI.SOD_AUTO_KEY__c, ROI);
system.debug('Map key:'+ m.get(ROI.SOD_AUTO_KEY__c) );


}


}

//Looking for the Sales Order Item that matches the Repair Order SOD Auto Key
List<SO_Item__c> soilst = [SELECT SOId__c, SOD_AUTO_KEY__c
FROM SO_Item__c
WHERE SOD_AUTO_KEY__c in :m.keySet() ];

//Write the ID of the Sales Order into the Repair Order. Field SO_ID__c under RO_Item__c
for(SO_Item__c b :soilst){

if (b.SOD_AUTO_KEY__c!=Null){

m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
system.debug('SO ID to be inserted:'+ b.SOId__c );

}


}

}


I've tried debugging it and I'm getting the NullPointerException on the line containing: m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;



What am I doing wrong?



Thanks in advance.







apex update before-trigger null-pointer insert






share|improve this question







New contributor




Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









Alejandro Flores

111




111




New contributor




Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Alejandro Flores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Are you sure that the values of b.SOD_AUTO_KEY__c and ROI.SOD_AUTO_KEY__c will be always same? Are you getting exception in this line -- m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;?
    – Jayant Das
    1 hour ago




















  • Are you sure that the values of b.SOD_AUTO_KEY__c and ROI.SOD_AUTO_KEY__c will be always same? Are you getting exception in this line -- m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;?
    – Jayant Das
    1 hour ago


















Are you sure that the values of b.SOD_AUTO_KEY__c and ROI.SOD_AUTO_KEY__c will be always same? Are you getting exception in this line -- m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;?
– Jayant Das
1 hour ago






Are you sure that the values of b.SOD_AUTO_KEY__c and ROI.SOD_AUTO_KEY__c will be always same? Are you getting exception in this line -- m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;?
– Jayant Das
1 hour ago












4 Answers
4






active

oldest

votes

















up vote
3
down vote













If you cannot guarantee that a value is not null, you typically add a null check:



MyObject__c record = myMap.get(someKey);
if (record != null)
{
record.MyField__c = someValue;
}





share|improve this answer





















  • You just need some light and hopefully obvious renaming to fit this approach into your current code.
    – Adrian Larson
    1 hour ago


















up vote
1
down vote













Your issue seems to be on this line:



m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;




In your code, you have added entries in your map as below:



m.put(ROI.SOD_AUTO_KEY__c, ROI);


Now, when you are iterating over soilst and trying to fetch the key as:



m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;


The b.SOD_AUTO_KEY__c may not be present in the Map, and thus it would return null, causing an exception when you try to fetch SO_ID__c from a null value.



You should instead try something as (change as required, but the intent is to check for null value before performing any operation on it):



if(m.get(b.SOD_AUTO_KEY__c) != null) {
m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
}





share|improve this answer



















  • 1




    You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
    – Adrian Larson
    1 hour ago










  • That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it.
    – Jayant Das
    1 hour ago


















up vote
1
down vote













Others have covered how to address the Null Pointer Exception (NPE for short). I'd like to cover how to determine the cause of an NPE.



When debugging NPEs, I like to keep in mind the famous saying of Sir Arthur Conan Doyle's Sherlock Holmes




when you eliminate the impossible, whatever remains, however improbable, must be the truth




An NPE occurs when you try to do something like null.method() or null.variable.
Something in your indicated statement is null. We need to suss out what that something is.



Given the statement



m.get(b.SOD_AUTO_KEY__c).SO_ID__c = b.SOId__c;


The things that might possibly be null are:




  • Your map m

  • Your variable b

  • Whatever you get as a result of m.get(b.SOD_AUTO_KEY__c)


As a side note, these variable names should probably be re-evaluated. Naming the variables in a way that describes what they are/do will help other people understand your code (and will help you understand your code, once enough time has passed that you forget exactly what you did).



Something like m => autoKeyToSOItemMap and b => soItem



Getting back on topic, m cannot possibly be null, because you initialize this variable in the same line on which it is declared. Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();. It can be an empty map, but empty and null are different things.



Likewise, b cannot possibly be null. This is the loop variable of your for loop. b is fed from soilist, which itself is populated with results from a query. A List<SO_Item__c> can contain null records, but the results of a query will never return a null record. Further evidence for this is that you execute b.SOD_AUTO_KEY__c != Null on the line above the one that your error is indicated on.



So, that leaves us with one possibility. Whatever the value of b.SOD_AUTO_KEY__c in the iteration of your loop that you're failing on, this value is not contained in your map m. This causes m.get(b.SOD_AUTO_KEY__c) to evaluate to null, and null.SO_ID__c is what throws your exception.






share|improve this answer




























    up vote
    0
    down vote













    This might be returning null. m.get(b.SOD_AUTO_KEY__c)



    Try to debug m and inspect the keys.






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "459"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      Alejandro Flores is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242531%2ffatal-errorsystem-nullpointerexception-attempt-to-de-reference-a-null-object%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      If you cannot guarantee that a value is not null, you typically add a null check:



      MyObject__c record = myMap.get(someKey);
      if (record != null)
      {
      record.MyField__c = someValue;
      }





      share|improve this answer





















      • You just need some light and hopefully obvious renaming to fit this approach into your current code.
        – Adrian Larson
        1 hour ago















      up vote
      3
      down vote













      If you cannot guarantee that a value is not null, you typically add a null check:



      MyObject__c record = myMap.get(someKey);
      if (record != null)
      {
      record.MyField__c = someValue;
      }





      share|improve this answer





















      • You just need some light and hopefully obvious renaming to fit this approach into your current code.
        – Adrian Larson
        1 hour ago













      up vote
      3
      down vote










      up vote
      3
      down vote









      If you cannot guarantee that a value is not null, you typically add a null check:



      MyObject__c record = myMap.get(someKey);
      if (record != null)
      {
      record.MyField__c = someValue;
      }





      share|improve this answer












      If you cannot guarantee that a value is not null, you typically add a null check:



      MyObject__c record = myMap.get(someKey);
      if (record != null)
      {
      record.MyField__c = someValue;
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 1 hour ago









      Adrian Larson

      104k19112235




      104k19112235












      • You just need some light and hopefully obvious renaming to fit this approach into your current code.
        – Adrian Larson
        1 hour ago


















      • You just need some light and hopefully obvious renaming to fit this approach into your current code.
        – Adrian Larson
        1 hour ago
















      You just need some light and hopefully obvious renaming to fit this approach into your current code.
      – Adrian Larson
      1 hour ago




      You just need some light and hopefully obvious renaming to fit this approach into your current code.
      – Adrian Larson
      1 hour ago












      up vote
      1
      down vote













      Your issue seems to be on this line:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;




      In your code, you have added entries in your map as below:



      m.put(ROI.SOD_AUTO_KEY__c, ROI);


      Now, when you are iterating over soilst and trying to fetch the key as:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;


      The b.SOD_AUTO_KEY__c may not be present in the Map, and thus it would return null, causing an exception when you try to fetch SO_ID__c from a null value.



      You should instead try something as (change as required, but the intent is to check for null value before performing any operation on it):



      if(m.get(b.SOD_AUTO_KEY__c) != null) {
      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
      }





      share|improve this answer



















      • 1




        You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
        – Adrian Larson
        1 hour ago










      • That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it.
        – Jayant Das
        1 hour ago















      up vote
      1
      down vote













      Your issue seems to be on this line:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;




      In your code, you have added entries in your map as below:



      m.put(ROI.SOD_AUTO_KEY__c, ROI);


      Now, when you are iterating over soilst and trying to fetch the key as:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;


      The b.SOD_AUTO_KEY__c may not be present in the Map, and thus it would return null, causing an exception when you try to fetch SO_ID__c from a null value.



      You should instead try something as (change as required, but the intent is to check for null value before performing any operation on it):



      if(m.get(b.SOD_AUTO_KEY__c) != null) {
      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
      }





      share|improve this answer



















      • 1




        You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
        – Adrian Larson
        1 hour ago










      • That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it.
        – Jayant Das
        1 hour ago













      up vote
      1
      down vote










      up vote
      1
      down vote









      Your issue seems to be on this line:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;




      In your code, you have added entries in your map as below:



      m.put(ROI.SOD_AUTO_KEY__c, ROI);


      Now, when you are iterating over soilst and trying to fetch the key as:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;


      The b.SOD_AUTO_KEY__c may not be present in the Map, and thus it would return null, causing an exception when you try to fetch SO_ID__c from a null value.



      You should instead try something as (change as required, but the intent is to check for null value before performing any operation on it):



      if(m.get(b.SOD_AUTO_KEY__c) != null) {
      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
      }





      share|improve this answer














      Your issue seems to be on this line:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;




      In your code, you have added entries in your map as below:



      m.put(ROI.SOD_AUTO_KEY__c, ROI);


      Now, when you are iterating over soilst and trying to fetch the key as:



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;


      The b.SOD_AUTO_KEY__c may not be present in the Map, and thus it would return null, causing an exception when you try to fetch SO_ID__c from a null value.



      You should instead try something as (change as required, but the intent is to check for null value before performing any operation on it):



      if(m.get(b.SOD_AUTO_KEY__c) != null) {
      m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 1 hour ago

























      answered 1 hour ago









      Jayant Das

      11.4k2523




      11.4k2523








      • 1




        You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
        – Adrian Larson
        1 hour ago










      • That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it.
        – Jayant Das
        1 hour ago














      • 1




        You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
        – Adrian Larson
        1 hour ago










      • That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it.
        – Jayant Das
        1 hour ago








      1




      1




      You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
      – Adrian Larson
      1 hour ago




      You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
      – Adrian Larson
      1 hour ago












      That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it.
      – Jayant Das
      1 hour ago




      That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it.
      – Jayant Das
      1 hour ago










      up vote
      1
      down vote













      Others have covered how to address the Null Pointer Exception (NPE for short). I'd like to cover how to determine the cause of an NPE.



      When debugging NPEs, I like to keep in mind the famous saying of Sir Arthur Conan Doyle's Sherlock Holmes




      when you eliminate the impossible, whatever remains, however improbable, must be the truth




      An NPE occurs when you try to do something like null.method() or null.variable.
      Something in your indicated statement is null. We need to suss out what that something is.



      Given the statement



      m.get(b.SOD_AUTO_KEY__c).SO_ID__c = b.SOId__c;


      The things that might possibly be null are:




      • Your map m

      • Your variable b

      • Whatever you get as a result of m.get(b.SOD_AUTO_KEY__c)


      As a side note, these variable names should probably be re-evaluated. Naming the variables in a way that describes what they are/do will help other people understand your code (and will help you understand your code, once enough time has passed that you forget exactly what you did).



      Something like m => autoKeyToSOItemMap and b => soItem



      Getting back on topic, m cannot possibly be null, because you initialize this variable in the same line on which it is declared. Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();. It can be an empty map, but empty and null are different things.



      Likewise, b cannot possibly be null. This is the loop variable of your for loop. b is fed from soilist, which itself is populated with results from a query. A List<SO_Item__c> can contain null records, but the results of a query will never return a null record. Further evidence for this is that you execute b.SOD_AUTO_KEY__c != Null on the line above the one that your error is indicated on.



      So, that leaves us with one possibility. Whatever the value of b.SOD_AUTO_KEY__c in the iteration of your loop that you're failing on, this value is not contained in your map m. This causes m.get(b.SOD_AUTO_KEY__c) to evaluate to null, and null.SO_ID__c is what throws your exception.






      share|improve this answer

























        up vote
        1
        down vote













        Others have covered how to address the Null Pointer Exception (NPE for short). I'd like to cover how to determine the cause of an NPE.



        When debugging NPEs, I like to keep in mind the famous saying of Sir Arthur Conan Doyle's Sherlock Holmes




        when you eliminate the impossible, whatever remains, however improbable, must be the truth




        An NPE occurs when you try to do something like null.method() or null.variable.
        Something in your indicated statement is null. We need to suss out what that something is.



        Given the statement



        m.get(b.SOD_AUTO_KEY__c).SO_ID__c = b.SOId__c;


        The things that might possibly be null are:




        • Your map m

        • Your variable b

        • Whatever you get as a result of m.get(b.SOD_AUTO_KEY__c)


        As a side note, these variable names should probably be re-evaluated. Naming the variables in a way that describes what they are/do will help other people understand your code (and will help you understand your code, once enough time has passed that you forget exactly what you did).



        Something like m => autoKeyToSOItemMap and b => soItem



        Getting back on topic, m cannot possibly be null, because you initialize this variable in the same line on which it is declared. Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();. It can be an empty map, but empty and null are different things.



        Likewise, b cannot possibly be null. This is the loop variable of your for loop. b is fed from soilist, which itself is populated with results from a query. A List<SO_Item__c> can contain null records, but the results of a query will never return a null record. Further evidence for this is that you execute b.SOD_AUTO_KEY__c != Null on the line above the one that your error is indicated on.



        So, that leaves us with one possibility. Whatever the value of b.SOD_AUTO_KEY__c in the iteration of your loop that you're failing on, this value is not contained in your map m. This causes m.get(b.SOD_AUTO_KEY__c) to evaluate to null, and null.SO_ID__c is what throws your exception.






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Others have covered how to address the Null Pointer Exception (NPE for short). I'd like to cover how to determine the cause of an NPE.



          When debugging NPEs, I like to keep in mind the famous saying of Sir Arthur Conan Doyle's Sherlock Holmes




          when you eliminate the impossible, whatever remains, however improbable, must be the truth




          An NPE occurs when you try to do something like null.method() or null.variable.
          Something in your indicated statement is null. We need to suss out what that something is.



          Given the statement



          m.get(b.SOD_AUTO_KEY__c).SO_ID__c = b.SOId__c;


          The things that might possibly be null are:




          • Your map m

          • Your variable b

          • Whatever you get as a result of m.get(b.SOD_AUTO_KEY__c)


          As a side note, these variable names should probably be re-evaluated. Naming the variables in a way that describes what they are/do will help other people understand your code (and will help you understand your code, once enough time has passed that you forget exactly what you did).



          Something like m => autoKeyToSOItemMap and b => soItem



          Getting back on topic, m cannot possibly be null, because you initialize this variable in the same line on which it is declared. Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();. It can be an empty map, but empty and null are different things.



          Likewise, b cannot possibly be null. This is the loop variable of your for loop. b is fed from soilist, which itself is populated with results from a query. A List<SO_Item__c> can contain null records, but the results of a query will never return a null record. Further evidence for this is that you execute b.SOD_AUTO_KEY__c != Null on the line above the one that your error is indicated on.



          So, that leaves us with one possibility. Whatever the value of b.SOD_AUTO_KEY__c in the iteration of your loop that you're failing on, this value is not contained in your map m. This causes m.get(b.SOD_AUTO_KEY__c) to evaluate to null, and null.SO_ID__c is what throws your exception.






          share|improve this answer












          Others have covered how to address the Null Pointer Exception (NPE for short). I'd like to cover how to determine the cause of an NPE.



          When debugging NPEs, I like to keep in mind the famous saying of Sir Arthur Conan Doyle's Sherlock Holmes




          when you eliminate the impossible, whatever remains, however improbable, must be the truth




          An NPE occurs when you try to do something like null.method() or null.variable.
          Something in your indicated statement is null. We need to suss out what that something is.



          Given the statement



          m.get(b.SOD_AUTO_KEY__c).SO_ID__c = b.SOId__c;


          The things that might possibly be null are:




          • Your map m

          • Your variable b

          • Whatever you get as a result of m.get(b.SOD_AUTO_KEY__c)


          As a side note, these variable names should probably be re-evaluated. Naming the variables in a way that describes what they are/do will help other people understand your code (and will help you understand your code, once enough time has passed that you forget exactly what you did).



          Something like m => autoKeyToSOItemMap and b => soItem



          Getting back on topic, m cannot possibly be null, because you initialize this variable in the same line on which it is declared. Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();. It can be an empty map, but empty and null are different things.



          Likewise, b cannot possibly be null. This is the loop variable of your for loop. b is fed from soilist, which itself is populated with results from a query. A List<SO_Item__c> can contain null records, but the results of a query will never return a null record. Further evidence for this is that you execute b.SOD_AUTO_KEY__c != Null on the line above the one that your error is indicated on.



          So, that leaves us with one possibility. Whatever the value of b.SOD_AUTO_KEY__c in the iteration of your loop that you're failing on, this value is not contained in your map m. This causes m.get(b.SOD_AUTO_KEY__c) to evaluate to null, and null.SO_ID__c is what throws your exception.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Derek F

          18.8k31848




          18.8k31848






















              up vote
              0
              down vote













              This might be returning null. m.get(b.SOD_AUTO_KEY__c)



              Try to debug m and inspect the keys.






              share|improve this answer

























                up vote
                0
                down vote













                This might be returning null. m.get(b.SOD_AUTO_KEY__c)



                Try to debug m and inspect the keys.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This might be returning null. m.get(b.SOD_AUTO_KEY__c)



                  Try to debug m and inspect the keys.






                  share|improve this answer












                  This might be returning null. m.get(b.SOD_AUTO_KEY__c)



                  Try to debug m and inspect the keys.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  ab0369

                  84




                  84






















                      Alejandro Flores is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Alejandro Flores is a new contributor. Be nice, and check out our Code of Conduct.













                      Alejandro Flores is a new contributor. Be nice, and check out our Code of Conduct.












                      Alejandro Flores is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Salesforce Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242531%2ffatal-errorsystem-nullpointerexception-attempt-to-de-reference-a-null-object%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                      Mangá

                       ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕