2

I am going slightly crazy over here. I want to pull the RecordType.Name off of the Opportunity and write some logic depending on what the name is.

public void onlineCalculatorShortenedLink(Map<Id, FPC_Funding_del__c> newfundIds, Map<Id, FPC_Funding_del__c> oldFundIds){
if (calculatorHelperClass.firstRun) {
    List<Id> fList = new List<Id>();
    for (Id f : newfundIds.keySet()) {
        system.debug('f:: ' + f);
        List<Opportunity> recordTypeNameList = [SELECT RecordTypeId, RecordType.Name FROM Opportunity WHERE Id = :newfundIds.get(f).Opportunity__c];
        String recordTypeName = recordTypeNameList[0].RecordType.Name;
        system.debug('recordtypename:: ' + recordTypeNameList + ', ' + recordTypeName);
        if (newfundIds.get(f).Lender_ID__r.Name.contains('National Funding') && (recordTypeName == 'Broker WC Oppty' || recordTypeName == 'Working Capital Oppty')){
            system.debug('record type names and lender worked');
            fList.add(f);
        }
}

I continue to get a error even though I can see with the debug that there is a value. What gives?

11:10:29.0 (111669296)|USER_DEBUG|[57]|DEBUG|calculatorHelperClass.firstRun:: true
11:10:29.0 (140108821)|USER_DEBUG|[58]|DEBUG|trigger new AND old:: {....too much data to include in this post....}
11:10:29.0 (140428329)|USER_DEBUG|[63]|DEBUG|f:: a0Dm0000004bPSmEAM
11:10:29.0 (140767494)|SOQL_EXECUTE_BEGIN|[64]|Aggregations:0|SELECT RecordTypeId, RecordType.Name FROM Opportunity WHERE Id = :tmpVar1
11:10:29.0 (144891311)|SOQL_EXECUTE_END|[64]|Rows:1
11:10:29.0 (145131119)|USER_DEBUG|[66]|DEBUG|recordtypename:: (Opportunity:{RecordTypeId=01280000000BZwlAAG, Id=006m00000093pC0AAI}), Working Capital Oppty
11:10:29.0 (145255596)|SYSTEM_MODE_EXIT|false
11:10:29.0 (145541450)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
  • 1
    Is your fList initialized? – Jayant Das Aug 23 '18 at 17:23
  • Yeah you've clearly demonstrated that you can dereference everything but flist. – Adrian Larson Aug 23 '18 at 17:24
  • Also it looks like your code snippet and debug logs are a bit out of sync. Because you debug statement within the if block should have been printed before the exception. Can you confirm if this is exactly how your code looks like? – Jayant Das Aug 23 '18 at 17:58
  • yea sorry I didn't include that in the snippet. flist is initialized. I will update with the whole code – Olivia Aug 23 '18 at 18:03
  • You really should bulkify your queries – Halfwarr Aug 23 '18 at 18:06
3

So looking at it I bet this is your problem:

newfundIds.get(f).Lender_ID__r.Name.contains('National Funding')

I'm guessing newfunds does not have a reference id for Lender_ID__r so when you do a .contains() on .Name you are really doing it on null which results in the error.

Note this is different if you were doing a simple == compare because you are asking for the String Object to call the method contains.

I also just wrote an anon apex to test this:

//Contact we are querying DOES NOT have a reference to an account
List<Contact> conList = [select id, name, Account.name from contact where id  = '003K000001FQCCp'];
Map<Id, Contact> mapCon = new Map<Id, Contact>();
for(Contact con : conList)
{   
    mapCon.put(con.id, con);
}

for(Id f : mapCon.keySet())
{   
    if(mapCon.get(f).Account.name == 'test')
    {
       // does not throw error 
    }

    if(mapCon.get(f).Account.name.contains('test'))
    {
       // throws error "Error Attempt to de-reference a null object"    
    }
}

To solve it you can use Short-circuit evaluation. Based on how you want to handle these type of records.

  • ah this is it. thank you! I need to change up the .contains() to == – Olivia Aug 23 '18 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.