+ Start a Discussion

Null Pointer Exception on wrapper class in SOQL sub-Query

My goal is to create a Visual Force table that can group child records by their parent records, and include a checkbox next to the child records to enable further logic when a button is clicked. So, I have figured out how to do wrapper classes to get the checkboxes working (without actually understanding them), and I have also done nested tables in a different  VF project. Combining the two has proven to be a real challenge for me, but I think I am really close now...I'm just getting stuck on a null pointer exception. I have included some comments in the code to hopefully help anybody who is interested in helping.





public with sharing class narrativeWizard_Controller {
	 	public String accountID;
	 	public String swpnAccount { get; set; }
        public SchoolWorkPlanLog__c getLog { get; set; }
        public List<ActionItemLogx__c> juncObs { get; set; }
        //public List<cAIS> actionItems { get; set; }  
        public List<cDS> theWrappers { get; set; }   
       	//populates the two wrapper classes defined below
        public List<cDS> getTheWrappers() {
                if(theWrappers == null) {
                    List<cAIS> actionItems = new List<cAIS>();
                    for(DeployedStrategy__c ds : [select ID, Name, Status__c, Strategy__r.Name, (select ID, Name, Name__c, Status__c, DeployedStrategy__r.Strategy__r.Name, EndDate__c from DeployedActionItem__r) from DeployedStrategy__c where DeployedStrategy__c.Account__c = :accountID And Status__c = 'Active']) {
                    	for(DeployedActionItems__c actI : ds.DeployedActionItem__r) {
                    		actionItems.add(new cAIS(actI));
                 	//This is where the null pointer exception is thrown. I system.debugged both ds and actionItems to check for values and found values in both. So not sure what the next debug step is.
                    	theWrappers.add(new cDS(ds, actionItems));                    	
                return theWrappers;      
        public PageReference processSelected() {  
        	//insert new Log
        	SchoolWorkPlanLog__c newLog = new SchoolWorkPlanLog__c(
        	Account__c = ApexPages.currentPage().getParameters().get('aID'),
        	Subject__c = this.getLog.Subject__c,
        	Narrative__c = this.getLog.Narrative__c,
        	InteractionDate__c = this.getLog.InteractionDate__c);
        	insert newLog;
        	//insert new Junction Records              
            List<DeployedActionItems__c> selectedAIs = new List<DeployedActionItems__c>();
            List<ActionItemLogx__c> newJuncObjs = new List<ActionItemLogx__c>();
            for(cDS cd : getTheWrappers()) {
                for(cAIS ca : cd.ais) {
                	if(ca.selected == true) {
            for(DeployedActionItems__c ds2 : selectedAIs) {
            	ActionItemLogx__c newJuncOb = new ActionItemLogx__c(
            	SchoolWorkPlanLog__c = newLog.Id,
            	ActionItem__c = ds2.Id);
            insert newJuncObjs;
            PageReference acctPage = new PageReference('/'+accountID);
            return acctPage;
        //sets up class extension of SchoolWorkPlanLog__c
        private final SchoolWorkPlanLog__c swpn;
        public narrativeWizard_Controller(ApexPages.StandardController swpnController) {
                this.accountID = ApexPages.currentPage().getParameters().get('aID');
                this.swpn = (SchoolWorkPlanLog__c)swpnController.getRecord();
                this.getLog= new SchoolWorkPlanLog__c();         
        public void setUpAccount() {
                Account acct = [select ID, Name from Account where ID = :accountID Limit 1];
                this.swpnAccount = acct.Name;
        //wrapper class for Deployed Strategies                
        public class cDS {
        	public DeployedStrategy__c wDS { get; set; }
        	public List<cAIS> ais = new List<cAIS>();
        	//wrapper constructor
        	public cDS(DeployedStrategy__c D, List<cAIS> aas) {
        		wDS = D;
        		ais = aas; 
        //wrapper class for Action Items      
        public class cAIS {
            public DeployedActionItems__c wAI { get; set; }
            public Boolean selected { get; set; }      
            //wrapper class constructor
            public cAIS(DeployedActionItems__c c) {
                    wAI = c;
                    selected = false;




Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You need to initialze


theWrappers = new List<cDS>();



After you check this condition and satisfies the condition


if(theWrappers == null) {


you can not add items to a null list which is not initialized. Hope it will work for you.