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
Molson94Molson94 

System.NullPointerException / Not sure why variable is being considered null

Hi EVeryone,

Stumped on why i keep getting the following error when testing my code block: System.NullPointerException: Attempt to de-reference a null object
I understand the reason for the error, but cannot seem to figure out why the code is throwing it. the Feedback object should be initialized and be able to be added to the feedbackList.

Appreciate the help!
 
String d = 'a0G190000015uMg'; //for testing

//containers for querys
Deliverable__c deliverable;
List<Feedback_Survey_Question__c> questionList;
List<Feedback_Responses__c> responseList;
List<Feedback__c> feedbackList;
List<Approver__c> approverList;

//query db for lists
deliverable = [SELECT id, type__c, milestone__c, Number_of_Concepts__c, Status__c FROM Deliverable__c WHERE id = :d];
approverList = [SELECT id, name, Approver__c, Approver_Role__c FROM Approver__c WHERE Deliverable__r.id = :d];
//assign pieces of delvierable

decimal concepts = deliverable.Number_of_Concepts__c;
string milestone = deliverable.milestone__c;

System.debug(deliverable);
System.debug(concepts);
System.debug(milestone);

for (Integer i = 1; i <= concepts; i++){ 
    for (Approver__c a : approverList) { 
    
    String role = a.Approver_Role__c;
    String aid = a.id;
    
    Feedback__c Feedback = new Feedback__c();
    Feedback.Name = 'Concept ' + i;
    Feedback.Deliverable__c = d;
    Feedback.Approver__c = aid;
    feedbackList.add(Feedback); //ERROR BEING THROWN HERE
    
    }
    
}


 
Best Answer chosen by Molson94
Mohit Bansal6Mohit Bansal6
Hi

You forgot to initialize a list of feedback. Kindly add below line, just before for loop.

feedbackList = List<Feedback__c>();

Regards
Mohit Bansal

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

All Answers

Mohit Bansal6Mohit Bansal6
Hi

You forgot to initialize a list of feedback. Kindly add below line, just before for loop.

feedbackList = List<Feedback__c>();

Regards
Mohit Bansal

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
This was selected as the best answer
KrForceKrForce
Anil MadithatiI think your "approverList" is coming as blank, check for list size before going into the loop.
FYi..I have added the check.

 

String d = 'a0G190000015uMg'; //for testing

//containers for querys
Deliverable__c deliverable;
List<Feedback_Survey_Question__c> questionList;
List<Feedback_Responses__c> responseList;
List<Feedback__c> feedbackList;
List<Approver__c> approverList;

//query db for lists
deliverable = [SELECT id, type__c, milestone__c, Number_of_Concepts__c, Status__c FROM Deliverable__c WHERE id = :d];
approverList = [SELECT id, name, Approver__c, Approver_Role__c FROM Approver__c WHERE Deliverable__r.id = :d];
//assign pieces of delvierable

decimal concepts = deliverable.Number_of_Concepts__c;
string milestone = deliverable.milestone__c;

System.debug(deliverable);
System.debug(concepts);
System.debug(milestone);

for (Integer i = 1; i <= concepts; i++){ 
if(approverList.size()>0){ //added The check.
    for (Approver__c a : approverList) { 
    
    String role = a.Approver_Role__c;
    String aid = a.id;
    
    Feedback__c Feedback = new Feedback__c();
    Feedback.Name = 'Concept ' + i;
    Feedback.Deliverable__c = d;
    Feedback.Approver__c = aid;
    feedbackList.add(Feedback); //ERROR BEING THROWN HERE
    
    }
    }
}