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












3














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.



Edit:



So thanks to the comments below I saw the light and was able to get this working. Basically I changed the last part of the code because I re-analyzed my use case and something was broke in the logic. I had to double check the info coming over to make sure that nothing was null (and nothing was coming null) but the error was how I was extracting the information from the lists.



I know it may not be the most efficient code ever but I'm going to be improving this in later on.



I'm going to post the new code below but I want to explain the use case if anyone needs this kind of triggers in the future (for beginners).



In my use case I have Repair Orders which are coming from an ERP system and I need them in SFDC to create dashboards, report over that information and basically have that information related to our customers and on the go for our employees.



Each Repair Order Item (generic to a Quote Line Item) may have a Sales Order Item associated. This Sales Order Item has a unique key on the database and I have that as a field in a different object as an external key. This code checks whenever a Repair Order Item is inserted/updated if the Sales Order Detail (generic to Item), if any, exists in Salesforce and retrieves the Sales Order Header ID so we can have a report of Related Repair Order Items with Sales Order.



The code is the following:



    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){

for(RO_Item__c ROIitem:Trigger.New){

if (b.SOD_AUTO_KEY__c!=Null){

if (ROIitem.SOD_AUTO_KEY__c==b.SOD_AUTO_KEY__c){

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

}



}

}


}

}









share|improve this question
























  • 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
    Dec 13 at 20:41












  • Yes, they are always going to be the same. They are the database keys for the same record.
    – Alejandro Flores
    Dec 15 at 15:21
















3














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.



Edit:



So thanks to the comments below I saw the light and was able to get this working. Basically I changed the last part of the code because I re-analyzed my use case and something was broke in the logic. I had to double check the info coming over to make sure that nothing was null (and nothing was coming null) but the error was how I was extracting the information from the lists.



I know it may not be the most efficient code ever but I'm going to be improving this in later on.



I'm going to post the new code below but I want to explain the use case if anyone needs this kind of triggers in the future (for beginners).



In my use case I have Repair Orders which are coming from an ERP system and I need them in SFDC to create dashboards, report over that information and basically have that information related to our customers and on the go for our employees.



Each Repair Order Item (generic to a Quote Line Item) may have a Sales Order Item associated. This Sales Order Item has a unique key on the database and I have that as a field in a different object as an external key. This code checks whenever a Repair Order Item is inserted/updated if the Sales Order Detail (generic to Item), if any, exists in Salesforce and retrieves the Sales Order Header ID so we can have a report of Related Repair Order Items with Sales Order.



The code is the following:



    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){

for(RO_Item__c ROIitem:Trigger.New){

if (b.SOD_AUTO_KEY__c!=Null){

if (ROIitem.SOD_AUTO_KEY__c==b.SOD_AUTO_KEY__c){

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

}



}

}


}

}









share|improve this question
























  • 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
    Dec 13 at 20:41












  • Yes, they are always going to be the same. They are the database keys for the same record.
    – Alejandro Flores
    Dec 15 at 15:21














3












3








3







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.



Edit:



So thanks to the comments below I saw the light and was able to get this working. Basically I changed the last part of the code because I re-analyzed my use case and something was broke in the logic. I had to double check the info coming over to make sure that nothing was null (and nothing was coming null) but the error was how I was extracting the information from the lists.



I know it may not be the most efficient code ever but I'm going to be improving this in later on.



I'm going to post the new code below but I want to explain the use case if anyone needs this kind of triggers in the future (for beginners).



In my use case I have Repair Orders which are coming from an ERP system and I need them in SFDC to create dashboards, report over that information and basically have that information related to our customers and on the go for our employees.



Each Repair Order Item (generic to a Quote Line Item) may have a Sales Order Item associated. This Sales Order Item has a unique key on the database and I have that as a field in a different object as an external key. This code checks whenever a Repair Order Item is inserted/updated if the Sales Order Detail (generic to Item), if any, exists in Salesforce and retrieves the Sales Order Header ID so we can have a report of Related Repair Order Items with Sales Order.



The code is the following:



    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){

for(RO_Item__c ROIitem:Trigger.New){

if (b.SOD_AUTO_KEY__c!=Null){

if (ROIitem.SOD_AUTO_KEY__c==b.SOD_AUTO_KEY__c){

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

}



}

}


}

}









share|improve this question















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.



Edit:



So thanks to the comments below I saw the light and was able to get this working. Basically I changed the last part of the code because I re-analyzed my use case and something was broke in the logic. I had to double check the info coming over to make sure that nothing was null (and nothing was coming null) but the error was how I was extracting the information from the lists.



I know it may not be the most efficient code ever but I'm going to be improving this in later on.



I'm going to post the new code below but I want to explain the use case if anyone needs this kind of triggers in the future (for beginners).



In my use case I have Repair Orders which are coming from an ERP system and I need them in SFDC to create dashboards, report over that information and basically have that information related to our customers and on the go for our employees.



Each Repair Order Item (generic to a Quote Line Item) may have a Sales Order Item associated. This Sales Order Item has a unique key on the database and I have that as a field in a different object as an external key. This code checks whenever a Repair Order Item is inserted/updated if the Sales Order Detail (generic to Item), if any, exists in Salesforce and retrieves the Sales Order Header ID so we can have a report of Related Repair Order Items with Sales Order.



The code is the following:



    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){

for(RO_Item__c ROIitem:Trigger.New){

if (b.SOD_AUTO_KEY__c!=Null){

if (ROIitem.SOD_AUTO_KEY__c==b.SOD_AUTO_KEY__c){

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

}



}

}


}

}






apex update before-trigger null-pointer insert






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked Dec 13 at 20:37









Alejandro Flores

183




183












  • 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
    Dec 13 at 20:41












  • Yes, they are always going to be the same. They are the database keys for the same record.
    – Alejandro Flores
    Dec 15 at 15:21


















  • 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
    Dec 13 at 20:41












  • Yes, they are always going to be the same. They are the database keys for the same record.
    – Alejandro Flores
    Dec 15 at 15:21
















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
Dec 13 at 20:41






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
Dec 13 at 20:41














Yes, they are always going to be the same. They are the database keys for the same record.
– Alejandro Flores
Dec 15 at 15:21




Yes, they are always going to be the same. They are the database keys for the same record.
– Alejandro Flores
Dec 15 at 15:21










4 Answers
4






active

oldest

votes


















4














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





















  • Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution.
    – Alejandro Flores
    2 days ago



















4














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
    Dec 13 at 20:45










  • Thank you, I'm going to review everything again and I'll get back with my findings.
    – Alejandro Flores
    Dec 15 at 15:22



















1














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
    Dec 13 at 20:45










  • 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
    Dec 13 at 20:46



















-1














Check for



if((m.get(b.SOD_AUTO_KEY__c)) != null){

}


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',
    autoActivateHeartbeat: false,
    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
    });


    }
    });














    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









    4














    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





















    • Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution.
      – Alejandro Flores
      2 days ago
















    4














    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





















    • Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution.
      – Alejandro Flores
      2 days ago














    4












    4








    4






    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 Dec 13 at 21:02









    Derek F

    18.9k31849




    18.9k31849












    • Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution.
      – Alejandro Flores
      2 days ago


















    • Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution.
      – Alejandro Flores
      2 days ago
















    Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution.
    – Alejandro Flores
    2 days ago




    Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution.
    – Alejandro Flores
    2 days ago













    4














    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
      Dec 13 at 20:45










    • Thank you, I'm going to review everything again and I'll get back with my findings.
      – Alejandro Flores
      Dec 15 at 15:22
















    4














    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
      Dec 13 at 20:45










    • Thank you, I'm going to review everything again and I'll get back with my findings.
      – Alejandro Flores
      Dec 15 at 15:22














    4












    4








    4






    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 Dec 13 at 20:43









    Adrian Larson

    104k19112235




    104k19112235












    • You just need some light and hopefully obvious renaming to fit this approach into your current code.
      – Adrian Larson
      Dec 13 at 20:45










    • Thank you, I'm going to review everything again and I'll get back with my findings.
      – Alejandro Flores
      Dec 15 at 15:22


















    • You just need some light and hopefully obvious renaming to fit this approach into your current code.
      – Adrian Larson
      Dec 13 at 20:45










    • Thank you, I'm going to review everything again and I'll get back with my findings.
      – Alejandro Flores
      Dec 15 at 15:22
















    You just need some light and hopefully obvious renaming to fit this approach into your current code.
    – Adrian Larson
    Dec 13 at 20:45




    You just need some light and hopefully obvious renaming to fit this approach into your current code.
    – Adrian Larson
    Dec 13 at 20:45












    Thank you, I'm going to review everything again and I'll get back with my findings.
    – Alejandro Flores
    Dec 15 at 15:22




    Thank you, I'm going to review everything again and I'll get back with my findings.
    – Alejandro Flores
    Dec 15 at 15:22











    1














    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
      Dec 13 at 20:45










    • 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
      Dec 13 at 20:46
















    1














    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
      Dec 13 at 20:45










    • 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
      Dec 13 at 20:46














    1












    1








    1






    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 Dec 13 at 20:48

























    answered Dec 13 at 20:44









    Jayant Das

    12k2623




    12k2623








    • 1




      You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
      – Adrian Larson
      Dec 13 at 20:45










    • 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
      Dec 13 at 20:46














    • 1




      You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
      – Adrian Larson
      Dec 13 at 20:45










    • 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
      Dec 13 at 20:46








    1




    1




    You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
    – Adrian Larson
    Dec 13 at 20:45




    You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null.
    – Adrian Larson
    Dec 13 at 20:45












    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
    Dec 13 at 20:46




    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
    Dec 13 at 20:46











    -1














    Check for



    if((m.get(b.SOD_AUTO_KEY__c)) != null){

    }


    Try to debug m and inspect the keys.






    share|improve this answer




























      -1














      Check for



      if((m.get(b.SOD_AUTO_KEY__c)) != null){

      }


      Try to debug m and inspect the keys.






      share|improve this answer


























        -1












        -1








        -1






        Check for



        if((m.get(b.SOD_AUTO_KEY__c)) != null){

        }


        Try to debug m and inspect the keys.






        share|improve this answer














        Check for



        if((m.get(b.SOD_AUTO_KEY__c)) != null){

        }


        Try to debug m and inspect the keys.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered Dec 13 at 20:42









        ab0369

        357




        357






























            draft saved

            draft discarded




















































            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

            Список кардиналов, возведённых папой римским Каликстом III

            Deduzione

            Mysql.sock missing - “Can't connect to local MySQL server through socket”