Pregunta

I am fairly new to Java and am trying to load a LinkedHashMap that contains an ArrayList of values. I am trying to load the values from a query result from an API based query result (Salesforce).

Here is the error: "Cannot refer to a non-final variable breakdown inside an inner class defined in a different method" - the breakdown variable is underlined in red giving this message, Ive noted the line in concern below.

CODE

public LinkedHashMap<String, ArrayList<String>> sfFundIdsByContact;

    public ArrayList<String> getFundsIDsForContact(Contact aContact)
    {
        QueryResult queryResults = null;
        ArrayList<String> ids = new ArrayList<String>();
        int index = 0;
        Boolean done = false;
        String contactid = aContact.getId();
        String SCCPBId = null;

        if(sfFundIdsByContact == null || sfFundIdsByContact.size() <= 0){

       //Do the Salesforce API CALL and Return the results  
       ...          
       while (! done) 
       {        
        SObject[] records = queryResults.getRecords();

        for ( int i = 0; i < records.length; ++i ) 
            {
                    if(sfFundIdsByContact.containsKey(breakdown.getSalesConnect__Contact__c())){
                        sfFundIdsByContact.get(breakdown.getSalesConnect__Contact__c()).add(breakdown.getId());
                    } else {
//Line below in the add(breakdown.getId() - contains the error
                    sfFundIdsByContact.put(breakdown.getSalesConnect__Contact__c(), new ArrayList<String>() {{ add(breakdown.getId()); }});

        }

    }

All suggestions are appreciated.

¿Fue útil?

Solución

In your else block, instead of:

new ArrayList<String>() {{ add(**breakdown.getId()**); }}

you can use:

new ArrayList<String>(Arrays.asList(breakdown.getId())

or, since you just want a single element ArrayList, you can use Collections.singletonList that avoids the creation of temporary varargs array:

new ArrayList<String>(Collections.singletonList(breakdown.getId())

The { ... } after the new ArrayList<>() creates an anonymous subclass of ArrayList, which is an inner class only. Inside an inner class you cannot access non-final local variables.

Otros consejos

You can ease the code by always retrieving the List value in the for loop, then if it is null create a new one and add it to your Map, otherwise add the value to the list.

for (int i = 0; i < records.length; i++) {
    List<String> value = sfFundIdsByContact.get(breakdown.getSalesConnect__Contact__c());
    if (value == null) {
        value = new ArrayList<String>();
        sfFundIdsByContact.put(breakdown.getSalesConnect__Contact__c(), value);
    }
    value.add(breakdown.getId());
}

As a recommendation, change the definition of

LinkedHashMap<String, ArrayList<String>> sfFundIdsByContact

to

Map<String, List<String>> sfFundIdsByContact

Refer to What does it mean to "program to an interface"?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top