1

I admit that I haven't done too much with the AggregateResult object but I can't figure out how to successfully extract the values to add to a Set.

Here's my code:

public List<AggregateResult> nonUniqueAcctStringCollection {get;set;}
public Set<String> acctNameStrings {get;set;}

public DataCleanupDashboardController() {
    nonUniqueAcctStringCollection = [select name from Account group by name having count(name)>1];
    system.debug('nonUniqueAcctStringCollection= '+nonUniqueAcctStringCollection);
    for(AggregateResult ar : nonUniqueAcctStringCollection){
            //acctNameStrings.add(String.ValueOf(ar.get('Name'))); - NULL pointer exception
            //acctNameStrings.add((String)ar.get('Name')); - NULL pointer exception
            system.debug(String.ValueOf(ar.get('Name')));
    }
}

However on my 2nd debug, all of the values are successfully outputted

  • 09:16:07:150 USER_DEBUG [13]|DEBUG|Account #1
  • 09:16:07:150 USER_DEBUG [13]|DEBUG|Account #2 ...
1

You need to verify Null before performing any operation on the object.

So, ar.get('Name') is returning null and you are using String.valueOf(), that's why it is giving NullPointer Exception.

Secondly, instantiate acctNameStrings variable before adding items to this Set.

public List<AggregateResult> nonUniqueAcctStringCollection {get;set;}
public Set<String> acctNameStrings {get;set;}

public DataCleanupDashboardController() 
{
    acctNameStrings = new Set<String>();
    nonUniqueAcctStringCollection = [select name from Account group by name having count(name)>1];
    system.debug('nonUniqueAcctStringCollection= '+nonUniqueAcctStringCollection);
    for(AggregateResult ar : nonUniqueAcctStringCollection)
    {
        if(ar.get('Name') !=null)
        {
            acctNameStrings.add(String.ValueOf(ar.get('Name')));
            System.debug(String.ValueOf(ar.get('Name')));
        }   
    }
}
| improve this answer | |
  • I tried that... it even fails on the if(ar.get('Name') !=null) – rmarq423 Jun 2 '17 at 14:37
  • 1
    it is failing because of acctNameStrings – Santanu Boral Jun 2 '17 at 14:39
  • Thanks... should have instantiated - not sure how I missed that. – rmarq423 Jun 2 '17 at 14:41

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.