0

My below code is getting a NullPointerException:

Line: 38, Column: 1 System.NullPointerException

LINE 38

Account_List_vod__c acctListRec = nameToAcctListMap.get(affRec.To_Account_Value__c);

I am unsure why I am getting this NullException. Here is my complete code:

public with sharing class Eisai_InsertAccountListItem_cls {
    List<Account_List_Item_vod__c> toInsertAcctListItem = new  List<Account_List_Item_vod__c>();
    List<Affiliation_vod__c> allAffiliationList = new List<Affiliation_vod__c>();
    List<Affiliation_vod__c> allChildAffList = new List<Affiliation_vod__c>();
    Map<String,Account_List_vod__c> nameToAcctListMap = new Map<String,Account_List_vod__c>();
    List<User> usersIdList; 
    Set<Id> usersIdSet = new Set<Id>();
    Set<String> affToAcctValSet = new Set<String>();
    Set<String> allHOAccountListName = new Set<String>();
    Integer totalRecordsInserted = 0, totalRecordsFailed = 0;

    public Eisai_InsertAccountListItem_cls(){

        allAffiliationList = [SELECT ID, OwnerId, Name, From_Account_vod__c, Account_List_Unique__c, To_Account_Value__c 
                                                                                FROM Affiliation_vod__c 
                                                                                WHERE Parent_vod__c = false
                                                                                AND To_Account_Value__c != Null
                                                                                AND From_Account_RecType__c = 'Professional_vod'
                                                                                AND OwnerId IN:getActiveUsers()];

        for (Affiliation_vod__c AffiliationList: allAffiliationList)
        {
                allChildAffList.add(AffiliationList);
                affToAcctValSet.add(AffiliationList.To_Account_Value__c);
        }

        for (Account_List_vod__c allAcctListQuery : [SELECT Id, OwnerId, Name, Name_Substring__c, Name_Populate_Unique_Owner__c 
                                                                                    FROM Account_List_vod__c 
                                                                                    WHERE Name_Substring__c IN :affToAcctValSet]){
            nameToAcctListMap.put(allAcctListQuery.Name_Substring__c, allAcctListQuery);
        }

        System.debug('Account List Map count: ' + nameToAcctListMap.size());

        for(Affiliation_vod__c affRec: allChildAffList){
                // NullException is happening here 
                Account_List_vod__c acctListRec = nameToAcctListMap.get(affRec.To_Account_Value__c);

            if(affRec.Account_List_Unique__c.equals(acctListRec.Name_Populate_Unique_Owner__c)){               
                        Account_List_Item_vod__c AccListItem = new Account_List_Item_vod__c(
                            Account_List_vod__c = acctListRec.Id,
                            Account_vod__c = affRec.From_Account_vod__c
                        );
                        toInsertAcctListItem.add(AccListItem);
            }
        }

        for(Database.SaveResult result: Database.insert(toInsertAcctListItem,false)) {
            if(result.isSuccess()) {
                totalRecordsInserted++;
            } else {
                totalRecordsFailed++;
            }
        }

        System.debug('Records successfully inserted: ' + totalRecordsInserted);
        System.debug('Records failed insertion: ' + totalRecordsFailed);

    } //end of Constructor

    public Set<Id> getActiveUsers() {

        usersIdList = new List<User>([SELECT Id
                                    FROM User
                                    WHERE (Profile_Name_vod__c LIKE '%Eisai_Epilepsy%' 
                                            OR Profile_Name_vod__c LIKE '%Eisai_PrimaryCare%'
                                            OR Profile_Name_vod__c LIKE '%Eisai_System%') 
                                    AND IsActive = TRUE]); 

        for(User users : usersIdList){
            usersIdSet.add(users.Id);  
        }

        return usersIdSet;
    }
} //end of Class
  • Can you double check the line number? I can't see why the one you've marked would cause an NPE and line 38 of the code you posted is a few lines further down. – Keith C Jul 22 '16 at 16:21
  • Before putting allAcctListQuery.Name_Substring__c in map, check the null – Santanu Boral Jul 22 '16 at 16:28
  • You have some truly terrible naming going on here... nameToAcctListMap... – Adrian Larson Jul 22 '16 at 16:47
  • You could be reporting an off-by-one line number...are you sure affRec.Account_List_Unique__c is not nillable? – Adrian Larson Jul 22 '16 at 16:53
  • You need to put in System.Debugs for your lists, something is empty – EricSSH Jul 22 '16 at 16:54
2

My guess is that this line is the actual culprit:

affRec.Account_List_Unique__c.equals(acctListRec.Name_Populate_Unique_Owner__c)

If your affRec record has a null value in the Account_List_Unique__c, you will get a NullPointerException. You can use == comparison if that's the case, as it won't explode if either side is null.

affRec.Account_List_Unique__c == acctListRec.Name_Populate_Unique_Owner__c

Note that equals comparison is case-sensitive, while == comparison is case-insensitive. If that's a deal breaker, you can simply check nulls first:

Boolean doesMatch = affRec.Account_List_Unique__c == null ?
    acctListRec.Name_Populate_Unique_Owner__c == null :
    affRec.Account_List_Unique__c.equals(acctListRec.Name_Populate_Unique_Owner__c);
  • Agreed although I think he should check for nulls either way – Griffin Jul 22 '16 at 17:18
  • @Griffin I tried what you have suggested answer above. Good thing, the error was gone, bad thing is no single account list item was created. Anythought? In the affiliation records, if they affiliated 1 Professional, there is a trigger creating to affiliation records, 1 is Parent = True and the other is Parent = False. Now, if there are 5 Professionals affiliated in a Group Practice account, there should be 10 affiliation records created. – Mariones Jul 22 '16 at 17:35
  • @Noiram It means that your if condition is always false. Sounds like a data problem. – Adrian Larson Jul 22 '16 at 17:38
  • The Account list is created depending on how many territories are sharing for a group practice account. So there will be a duplicate Account list name but different owner (User), to avoid duplicate creation of account list, I created a workflow to populate the Name_Populate_Unique_Owner__c field (Case sensitive) after insert of Account List record. – Mariones Jul 22 '16 at 17:39
0

The solution here is very simple. You need to check to make sure you have all the variables you are using before you try using them. Here's what I recommend:

    public Eisai_InsertAccountListItem_cls(){
    allAffiliationList = [SELECT ID, OwnerId, Name, From_Account_vod__c, Account_List_Unique__c, To_Account_Value__c 
                                                                            FROM Affiliation_vod__c 
                                                                            WHERE Parent_vod__c = false
                                                                            AND To_Account_Value__c != Null
                                                                            AND From_Account_RecType__c = 'Professional_vod'
                                                                            AND OwnerId IN:getActiveUsers()];

    for (Affiliation_vod__c AffiliationList: allAffiliationList)
    {
            allChildAffList.add(AffiliationList);
            affToAcctValSet.add(AffiliationList.To_Account_Value__c);
    }
If(!affToAccValSet.isEmpty()){
    List<Account_List_vod__c> FirstCheck = [SELECT Id, OwnerId, Name, Name_Substring__c, Name_Populate_Unique_Owner__c 
                                                                                FROM Account_List_vod__c 
                                                                                WHERE Name_Substring__c IN :affToAcctValSet];

If(!FirstCheck.isEmpty()){
    for (Account_List_vod__c allAcctListQuery : [SELECT Id, OwnerId, Name, Name_Substring__c, Name_Populate_Unique_Owner__c 
                                                                                FROM Account_List_vod__c 
                                                                                WHERE Name_Substring__c IN :affToAcctValSet]){
        **nameToAcctListMap.put(allAcctListQuery.Name_Substring__c, allAcctListQuery);**
    }
}
}}
    System.debug('Account List Map count: ' + nameToAcctListMap.size());

    for(Affiliation_vod__c affRec: allChildAffList){
    if(nameToAcctListMap.get(affRec.To_Account_Value__c) != null){
            Account_List_vod__c acctListRec = **nameToAcctListMap.get(affRec.To_Account_Value__c);**

        if(affRec.Account_List_Unique__c.equals(acctListRec.Name_Populate_Unique_Owner__c)){               
                    Account_List_Item_vod__c AccListItem = new Account_List_Item_vod__c(
                          Account_List_vod__c = acctListRec.Id,
                          Account_vod__c = affRec.From_Account_vod__c
                    );
                    toInsertAcctListItem.add(AccListItem);
        }
}
    }

    for(Database.SaveResult result: Database.insert(toInsertAcctListItem,false)) {
        if(result.isSuccess()) {
            totalRecordsInserted++;
        } else {
            totalRecordsFailed++;
        }
    }

    System.debug('Records successfully inserted: ' + totalRecordsInserted);
    System.debug('Records failed insertion: ' + totalRecordsFailed);

Please note: I HIGHLY ENCOURAGE you do a COMPLETE refactoring of your code. I have seen this style before and it not maintainable and makes fixing errors like these difficult to say the least. This is compounded by Apex's lack of debugging tools. Try better using more methods, try-catch, and abstract structures. Also these checks will not work if the problem exists with your query-object relation (i.e. you queried a non-existent field / are referencing a non-existent field). You may also need more checks than the ones given. This is just to start you off.

Similar question was asked and answered on the Salesforce Developer board here.

Below is the code which I tried

public Eisai_InsertAccountListItem_cls(){

        allAffiliationList = [SELECT ID, OwnerId, Name, From_Account_vod__c, Account_List_Unique__c, To_Account_Value__c 
                                                                                FROM Affiliation_vod__c 
                                                                                WHERE Parent_vod__c = false
                                                                                AND To_Account_Value__c != Null
                                                                                AND From_Account_RecType__c = 'Professional_vod'
                                                                                AND OwnerId IN:getActiveUsers()];

        for (Affiliation_vod__c AffiliationList: allAffiliationList)
        {
                allChildAffList.add(AffiliationList);
                affToAcctValSet.add(AffiliationList.To_Account_Value__c);
        }


        If(!affToAcctValSet.isEmpty()){
            List<Account_List_vod__c> FirstCheck = [SELECT Id, OwnerId, Name, Name_Substring__c, Name_Populate_Unique_Owner__c 
                                                                                        FROM Account_List_vod__c 
                                                                                        WHERE Name_Substring__c IN :affToAcctValSet];

            If(!FirstCheck.isEmpty()){
                for (Account_List_vod__c allAcctListQuery : [SELECT Id, OwnerId, Name, Name_Substring__c, Name_Populate_Unique_Owner__c 
                                                                                            FROM Account_List_vod__c 
                                                                                            WHERE Name_Substring__c IN :affToAcctValSet]){
                    nameToAcctListMap.put(allAcctListQuery.Name_Populate_Unique_Owner__c, allAcctListQuery);
                }
            }
        }
        System.debug('Account List Map count: ' + nameToAcctListMap.size());

        for(Affiliation_vod__c affRec: allChildAffList){
            if(nameToAcctListMap.get(affRec.To_Account_Value__c) != null){
                    Account_List_vod__c acctListRec = nameToAcctListMap.get(affRec.Account_List_Unique__c);

                if(affRec.Account_List_Unique__c == acctListRec.Name_Populate_Unique_Owner__c){               
                            Account_List_Item_vod__c AccListItem = new Account_List_Item_vod__c(
                                  Account_List_vod__c = acctListRec.Id,
                                  Account_vod__c = affRec.From_Account_vod__c
                            );
                            toInsertAcctListItem.add(AccListItem);
                }
            }
        }

        for(Database.SaveResult result: Database.insert(toInsertAcctListItem,false)) {
            if(result.isSuccess()) {
                totalRecordsInserted++;
            } else {
                totalRecordsFailed++;
            }
        }

        System.debug('Records successfully inserted: ' + totalRecordsInserted);
        System.debug('Records failed insertion: ' + totalRecordsFailed);

BUT, it's giving me this log in the developer console.

NOTE: There are records count in the query. AnythoughtS? enter image description here

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.