function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KitagawaSan85KitagawaSan85 

Attempt to de-reference a null object

I am working on a controller extention to create some values for a visualforce page but and running into an issue when trying to count the number of chatter posts for a given account hierarchy that have a certain tag. 

 

I can get it to return the total feeditems for an account using an SOQL query, but run into an issue when I try to pull in the feeds for multiple accounts using 'WHERE ParentID IN :ListOfAccounts'. 

 

Can anybody see if I am missing something?

List<FeedItem> Chatters = new List<FeedItem>();{
    for(FeedItem FI : [SELECT ID,body from FeedItem]) {
        if(FI.Body.contains('#CRTNEWS')) {
          Chatters.add(FI);
        }}}
        
    List<account> accts = [SELECT Id FROM Account WHERE NA_Code_EU__c = :acc.NA_Code_EU__c]; 
    
    public integer getChatter() {
        return [SELECT count() FROM AccountFeed WHERE ParentID IN :accts AND ID IN:Chatters];
    } 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
KitagawaSan85KitagawaSan85

Was able to modify to this: 

 

public integer getChatter() {
       List<Account> Accounts = [SELECT ID from Account WHERE Rep_Site_ID__c = :acc.Rep_Site_ID__c];
       List<ID> Chatters = new List<ID>();{
           for( FeedItem FI : [SELECT ID,Body FROM feedItem WHERE type='TextPost']) {
               if( FI.body.contains('#CRTNEWS')) {
                   Chatters.add(FI.id);
                   }}}
       if( Accounts == null && Chatters == null) {return 0;} else {return [SELECT count() FROM AccountFeed WHERE ParentID  IN :Accounts and ID IN :Chatters];}
        
    } 

 which works... 

All Answers

Jia HuJia Hu
AND ID IN:Chatters is wrong,...

turn the List<FeedItem> Chatters to List<ID> Chatters or Set<Id>
add only Id field to this List or Set
KitagawaSan85KitagawaSan85

Thanks, but that didnt seem to help. 

 

    List<Id> Chatters = new List<Id>();{
    for(FeedItem FI : [SELECT ID,body from FeedItem]) {
        if(FI.Body.contains('#CRTNEWS')) {
          Chatters.add(FI.id);
        }}}

Still returns a null object error. 

KitagawaSan85KitagawaSan85

Was able to modify to this: 

 

public integer getChatter() {
       List<Account> Accounts = [SELECT ID from Account WHERE Rep_Site_ID__c = :acc.Rep_Site_ID__c];
       List<ID> Chatters = new List<ID>();{
           for( FeedItem FI : [SELECT ID,Body FROM feedItem WHERE type='TextPost']) {
               if( FI.body.contains('#CRTNEWS')) {
                   Chatters.add(FI.id);
                   }}}
       if( Accounts == null && Chatters == null) {return 0;} else {return [SELECT count() FROM AccountFeed WHERE ParentID  IN :Accounts and ID IN :Chatters];}
        
    } 

 which works... 

This was selected as the best answer