+ Start a Discussion
Zander ZumbrunnenZander Zumbrunnen 

Can't access object field values from a map.

Hello,
I am currently creating an Apex class to pass filtered query results to an LWC.
I first query for accounts and then calculate that account's distance away from a certain point and put that number in another list (Couldn't figure out how to put that number into the field Distance__c and have it stay there when returned to my LWC, this might be the main problem). Then I want to sort by the shortest distance. To do this I put Distances in the DirtyMap as a key and the InitialResources as the value. Sort Distances using .sort() and fill in CleanMap with the new Distances as the key and the related accounts as the value. Here is said code.
@AuraEnabled(cacheable=true)
    public static Map<Double,Account> searchResources(String state, String type, Integer distance, Double latitude, Double longitude, String What_type_of_residence_do_you_live_in, String Are_you_having_financial_difficulties, String Can_you_pay_for_utilities, String Can_you_pay_for_housing, String Can_you_pay_for_your_medical_visits, String Can_you_pay_for_your_transportation, String Do_you_eat_at_least_two_meals_a_day, String Can_you_pay_for_food, String Can_you_pay_for_your_medication, String How_do_you_get_to_medical_appointments, String Do_you_feel_safe_at_home, String Police_called_to_home_in_last_90_days) {
        // Initialize distance variables
        Double lat1 = latitude;
        Double lon1 = longitude;
        Double lat2;
        Double lon2;
        Double x;
        Double y;
        Double xx;
        Double yy;
        Double a;
        Double c;
        Double d;

        // Get all resources and create lists and maps for data manipulation
        List<Account> InitialResources = [SELECT Id, Name, Phone, Description, Email__c, Distance__c, Latitude__c, Longitude__c, Food_Bank__c, Financial_Assistance__c, Shelter__c, Case_Management__c, Serves_Meals__c, Senior_Center__c, Housing_Assistance__c, Transportation__c
                                          FROM Account
                                          WHERE RecordTypeId = '0121R000000yZGLQA2' AND BillingState = :state];
        List<Double> Distances = new List<Double>();
        Map<Double,Account> DirtyMap = new Map<Double,Account>();
        Map<Double,Account> CleanMap = new Map<Double,Account>();
        Map<Double,Account> FilteredMap = new Map<Double,Account>();

        // Get distance in miles of each resource and add to Distance__c
        for (Account record : InitialResources){
            lat2 = record.Latitude__c;
            lon2 = record.Longitude__c;
            x = lat1 * 3.141592 / 180;
            y = lat2 * 3.141592 / 180;
            xx = (lat2 - lat1) * 3.141592 / 180;
            yy = (lon2 - lon1) * 3.141592 / 180;
            a = math.sin(xx/2) * math.sin(xx/2) + math.cos(x) * math.cos(y) * math.sin(yy/2) * math.sin(yy/2);
            c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a));
            d = 6371000 * c;
            d = d * 0.00062137;
            Distances.add(d);
        }

        // Sort InitialResources by Distance__c
        for (Integer i = 0; i < InitialResources.size(); i++){
            if (Distances[i] <= distance)
                DirtyMap.put(Distances[i], InitialResources[i]);
        }
        Distances.sort();
        for (Integer i = 0; i < InitialResources.size(); i++){
            CleanMap.put(Distances[i], DirtyMap.get(Distances[i]));
        }

        // Return resources depenging on filters
        if (type == 'Recommended Resources') {
            for (Double key : CleanMap.keySet()) {
                if (CleanMap.get(key).Food_Bank__c && Can_you_pay_for_food == 'No')
                    FilteredMap.put(key, CleanMap.get(key));
            }
            return FilteredMap;
        }
        else
            return CleanMap;
    }
}
The problem area is the underlined part in the last "If" statement.
For some reason when I try accessing that boolean field from the map value it returns this error "Attempt to de-reference a null object".

I don't think I'm doing anything wrong since I have tried a few different things using the map values. I could only get it to work when referencing the initial list which is not in the right order.
(Ex. InitialResouces[i].Food_Bank__c works as intended but CleanMap.get(key).Food_Bank__c does not.)

If that is just a quality of maps then I guess I need a workaround, but I can't seem to think of a way of sorting the account list based on the distance list without using a map.

Hopefully, my problem makes sense. 
Thanks in advance.