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
sfdc007sfdc007 

Attempt to de-reference a null object

Hi,

I have vf page and apex class where i am tracking the ols sub status value and new substatus in vf page

when i open my vf page i am getting the below error

Attempt to de-reference a null object
An unexpected error has occurred. Your development organization has been notified.


MY VF PAGE :
<apex:page standardController="Team_Form__c"  extensions="CycleTimeSubstatusCalulation" tabStyle="Team_Form__c" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="EMEA SaSu Cycle-Time"  mode="inlineEdit" >
          
           <apex:repeat value="{!gpsTeamForm}" var="gps" >
            <apex:outputText title="Initial sub-status :" value="{!gps.OldValue}"/>
            <apex:outputText title="New sub-status :" value="{!gps.NewValue}"/>
            <apex:outputText title="Cycle Time :" />
            </apex:repeat>
        </apex:pageblock>
    </apex:form>
</apex:page>

MY Apex class :

public class CycleTimeSubstatusCalulation{

    public  List<Team_Form__History>  gpsTeamForm {get;set;}
    public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
        Team_Form__c tf = (Team_Form__c) controller.getRecord();
        list<Team_Form__History > tempList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
                                                FROM Team_Form__History
        
                                       WHERE ParentId = :tf.Id
                                                 AND Field = 'SubStatus__c'];
        if(tempList .size()>0)
        for(integer i=0; i<tempList.size(); i++ )
        {
         gpsTeamForm.add(tempList[i]);
        }
        
            //gpsTeamForm = tempList[0];
        else
            gpsTeamForm = new Team_Form__History();
    }
}

Hellp me what i am doing wrong here

Thanks in Advance
 
Best Answer chosen by sfdc007
sandeep sankhlasandeep sankhla
Hi,

As a best practise we should always initalize all our variables before using them on page side and class side...Please initalize all you rvariables in constructor....
public class CycleTimeSubstatusCalulation{

    public  List<Team_Form__History>  gpsTeamForm {get;set;}

    public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {

    gpsTeamForm = new List<Team_Form__History>();
    list<Team_Form__History > tempList = new list<Team_Form__History >();

        Team_Form__c tf = (Team_Form__c) controller.getRecord();

        tempList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
                                                FROM Team_Form__History
        
                                       WHERE ParentId = :tf.Id
                                                 AND Field = 'SubStatus__c'];
        if(tempList .size()>0)
        for(integer i=0; i<tempList.size(); i++ )
        {
         gpsTeamForm.add(tempList[i]);
        }
        
            //gpsTeamForm = tempList[0];
        else
            gpsTeamForm = new Team_Form__History();
    }
}

Please check and let me know if it helps you..

Thanks, 
Sandeep

All Answers

sandeep sankhlasandeep sankhla
Hi,

As a best practise we should always initalize all our variables before using them on page side and class side...Please initalize all you rvariables in constructor....
public class CycleTimeSubstatusCalulation{

    public  List<Team_Form__History>  gpsTeamForm {get;set;}

    public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {

    gpsTeamForm = new List<Team_Form__History>();
    list<Team_Form__History > tempList = new list<Team_Form__History >();

        Team_Form__c tf = (Team_Form__c) controller.getRecord();

        tempList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
                                                FROM Team_Form__History
        
                                       WHERE ParentId = :tf.Id
                                                 AND Field = 'SubStatus__c'];
        if(tempList .size()>0)
        for(integer i=0; i<tempList.size(); i++ )
        {
         gpsTeamForm.add(tempList[i]);
        }
        
            //gpsTeamForm = tempList[0];
        else
            gpsTeamForm = new Team_Form__History();
    }
}

Please check and let me know if it helps you..

Thanks, 
Sandeep
This was selected as the best answer
sfdc007sfdc007
got it bro , thanks :)