1

I've been at this for a while and can't seem to figure out why I'm getting the error "System.NullPointerException: Attempt to de-reference a null object" for the "comments.add(com)" line below

    public list<Approval_Request__c> comments;
    public List<Approval_Request__c> getcomments(){

            for(Approval_Request__c com : [SELECT Id,
                                                  Approver_Comments__c
                                                  FROM Approval_Request__c
                                                  WHERE parentId__c IN (SELECT Id
                                                                        FROM HigherParent__c   
                                                                        WHERE Opportunity__c = :oppId)
                                                            ORDER BY CreatedDate DESC 
                                                           ]){
                                                           if(com.Approver_Comments__c.contains('Approved with comments')){
                                                               ***comments.add(com)***;}
                                                           }
            if(comments.size() > 0)
            return comments;
            return null;
1

The error is probably coming in

if(com.Approver_Comments__c.contains('Approved with comments')){.

One of the records returned by the SOQL query might have a null value in Approver_Comments__c field and there is a contains check on that. You would have to put a null check on this field before applying contains.

Something like this:

    if(!String.isEmpty(com.Approver_Comments__c) && com.Approver_Comments__c.contains('Approved with comments'))
    { 
       comments.add(com);
    }
| improve this answer | |
0

You have not initialized this list<Approval_Request__c> comments but you are trying to check the size using comments.size() and adding items into the list.

This will work:

public list<Approval_Request__c> comments = new List<Approval_Request__c>();
    public List<Approval_Request__c> getcomments(){

            for(Approval_Request__c com : [SELECT Id,
                                                            Approver_Comments__c
                                                            FROM Approval_Request__c
                                                            WHERE parentId__c IN (SELECT Id
                                                                                  FROM HigherParent__c   
                                                                                  WHERE Opportunity__c = :oppId)
                                                            ORDER BY CreatedDate DESC 
                                                           ]){
                                                           if(com.Approver_Comments__c.contains('Approved with comments')){
                                                               **comments.add(com)**;}
                                                           }
            if(comments.size() > 0)
            return comments;
            return null;
| improve this answer | |
  • Thanks Ashwani, I have tried this as well and still get the error. – Andrew Jun 8 '16 at 21:44

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.