+ Start a Discussion
aKallNVaKallNV 

Another Null Pointer Exception Post. Should be easy.

Hi,

I'm trying to make a page that overides an edit page of a custom object called SchoolWorkPlanLog__c.

This object has a many to many relationship with the nvpsDeployedStrategies__c object and I want to force my users to create at least one Junction Object(StrategyLog__c) record when creating a new SchoolWorkPlanLog__c.

 

I feel like I'm prettly close, but I'm running into a Null Pointer Exception on Line 24 of controller, which is the first line that references the getLog variable that is declared at the top. It's my understanding of making VF pages that the getLog variable should have all the data provided by the user from the input fields defined in the page, which is the data I want to insert in the processSelected method along with the JunctionObjects.

 

 

public with sharing class narrativeWizard_Controller {
        
        public String swpnAccount { get; set; }
        public SchoolWorkPlanLog__c getLog { get; set; }
        public List<StrategyLog__c> juncObs { get; set; }
        public List<cStrats> strategies { get; set; }     
        
        
        public List<cStrats> getStrategies() {
                if(strategies == null) {
                    strategies = new List<cStrats>();
                    for(nvpsDeployedStrategies__c ds : [select ID, Name, Status__c, Strategy__r.Name, EndDate__c from nvpsDeployedStrategies__c where Account__c = :ApexPages.currentPage().getParameters().get('aID') And Status__c = 'Active']) {
                            strategies.add(new cStrats(ds));
                    }
                }
                return strategies;      
        }
        
        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<nvpsDeployedStrategies__c> selectedStratys = new List<nvpsDeployedStrategies__c>();
            List<StrategyLog__c> newJuncObjs = new List<StrategyLog__c>();
               
            for(cStrats cStrat : getStrategies()) {
                if(cStrat.selected == true) {
                	selectedStratys.add(cStrat.wDS);
                }
            }
            
            for(nvpsDeployedStrategies__c ds2 : selectedStratys) {
            	StrategyLog__c newJuncOb = new StrategyLog__c(
            	SchoolWorkPlanNarrative__c = newLog.Id,
            	Strategy__c = ds2.Id);
            	
            	newJuncObjs.add(newJuncOb);
            }
            insert newJuncObjs;
                        
            return null;
        }
        
        
        //sets up class extension of SchoolWorkPlanLog__c
        private final SchoolWorkPlanLog__c swpn;
                
        public narrativeWizard_Controller(ApexPages.StandardController swpnController) {
                this.swpn = (SchoolWorkPlanLog__c)swpnController.getRecord();
                this.setUpAccount();  
                this.getStrategies();          
        }
        
        public void setUpAccount() {
                Account acct = [select ID, Name from Account where ID = :ApexPages.currentPage().getParameters().get('aID') Limit 1];
                this.swpnAccount = acct.Name;
        }
        
                        
        //wrapper class
        public class cStrats {
                public nvpsDeployedStrategies__c wDS { get; set; }
                public Boolean selected { get; set; }           
                
                //wrapper class constructor
                public cStrats(nvpsDeployedStrategies__c c) {
                        wDS = c;
                        selected = false;
                }
        }
}

 

 

 

<apex:page sidebar="false" showHeader="false" standardController="SchoolWorkPlanLog__c" extensions="narrativeWizard_Controller">
    <apex:form >
      <apex:pageBlock >       
      <apex:pageBlockSection >
          <apex:commandButton action="{!processSelected}" value="Save"/>
          <apex:outputText value="{!swpnAccount}"/>          
      </apex:pageBlockSection> 
      <apex:pageBlockSection >
          <apex:inputField value="{!getLog.Subject__c}"/>
          <apex:inputField value="{!getLog.Narrative__c}"/>
          <apex:inputField value="{!getLog.InteractionDate__c}"/>
      </apex:pageBlockSection>     
      <apex:pageBlockSection >
          <apex:PageBlockTable value="{!strategies}" var="c">
              <apex:column >
                  <apex:inputCheckbox value="{!c.selected}"/>
              </apex:column>
              <apex:column headerValue="Name">
                  {!c.wDS.Strategy__r.Name}
              </apex:column>
              <apex:column headerValue="End Date" width="125">                
                   <apex:outputText value="{0,date,MM-dd-yyyy}">
                   <apex:param value="{!c.wDS.EndDate__c}"/>
                   </apex:outputText>
              </apex:column>
          </apex:PageBlockTable>
      </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>   
</apex:page>

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The getLog variable will be initialised to null, so you'll need to create a new instance before you can dereference it to access fields etc.

 

In your constructor:

 

 

public narrativeWizard_Controller(ApexPages.StandardController swpnController) {
                this.swpn = (SchoolWorkPlanLog__c)swpnController.getRecord();
                this.setUpAccount();  
                this.getStrategies();  
                this.getLog=new SchoolWorkPlanLog__c();        
        }