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
pradyprady 

"System.NullPointerException: Attempt to de-reference a null object", Failure Stack Trace

Hi,

 

I have a class written and when i move the class to production i get an error. Everything seems to work fine on sandBox.

If i remove the block of code it passes the code to production. I have a almost similar piece of code just

below the erroring code. This works fine and gets passed over into production. Any ideas on what could

be the issue?

 

Failure Message: "System.NullPointerException: Attempt to de-reference a null object", Failure Stack Trace: "Class.AppointmentController.refresh: line 252, column 82 Class.Test_Appointment.Testapp: line 29, column 6 External entry point"

 

Here is my code

 

Public Class AppointmentController
{

    public PageReference createApp() {
    
        return page.Appointment;
    }

Public Appointment__c Appointment { get; set; }
public String Error { get; set; }
public String ClientsSearch { get; set; }
public list<CEventTimeSlot> liETimeSlots {get;set;}
public list<CEventTimeSlot> liETimeSlots_form {get;set;}
public list<CTimeSlot> liTimeSlots {get;set;}
public list<CTimeSlot> liTimeSlots_form {get;set;}
public Map<string,CTimeSlot> mapTimeToSlot {get;set;}
public Map<string,CEventTimeSlot> mapETimeToSlot {get;set;}
public List<String> LstString{get;set;}

Public AppointmentController()
{
Appointment = new  Appointment__c();
}
   


/* function called from VF page for updating the startdatetime and endDatetime  and rendering into VF page   */
   public PageReference refresh() {
    try
      {
         liTimeSlots = new list<CTimeSlot>();
         mapTimeToSlot = new Map<string,CTimeSlot>();
         liTimeSlots_form = new list<CTimeSlot>();
         //For adding Events
         liETimeSlots = new list<CEventTimeSlot>();
         mapETimeToSlot = new Map<string,CEventTimeSlot>();
         liETimeSlots_form = new list<CEventTimeSlot>();
          
          
          
         

        
      if (Appointment.Start_Date__c<>null)
      {
        Time pktime;
        Time pkTime1;
        List<Time> LstTime = new List<Time>();
        pktime=time.newInstance(7,0,0,0);
        LstTime.add(pktime);

        for(integer ctr=0; ctr<30;ctr++)
        {
            pktime=pktime.addMinutes(30);
            LstTime.Add(pkTime);
        }
        system.Debug(' Listtime '+LstTime);
        
        system.debug('No of Time Items'+LstTime.size());
        for(integer ctr1=0; ctr1<LstTime.size();ctr1++)
        {       
        
        CTimeSlot newSlot = new CTimeSlot(string.valueof(LstTime[ctr1]));
        liTimeSlots.add(newSlot);
        
        mapTimeToSlot.put(string.valueof(LstTime[ctr1])+ '', newSlot);
        newSlot=null;
        
        CEventTimeSlot newESlot = new CEventTimeSlot(string.valueof(LstTime[ctr1]));
        liETimeSlots.add(newESlot);
        
        mapETimeToSlot.put(string.valueof(LstTime[ctr1])+ '', newESlot);
        newESlot=null;
        }
        
        
		system.debug('liETimeSlots.....'+liETimeSlots);
		system.debug('mapETimeToSlot.....'+mapETimeToSlot);
        String t_startdatetime_str;
        String t_enddatetime_str;
        integer t_syear;
        
        integer t_smonth;
        integer t_day;
        t_syear=Appointment.Start_Date__c.year();
        t_smonth=Appointment.Start_Date__c.month();
        t_day=Appointment.Start_Date__c.day();
        t_startdatetime_str=t_syear+'-'+t_smonth+'-'+t_day+' 00:00:00';
        datetime t_startdatetime;
        datetime t_enddatetime;
        t_startdatetime=datetime.valueof(t_startdatetime_str);
        t_enddatetime=t_startdatetime.addHours(23);
        t_enddatetime=t_enddatetime.addMinutes(59);
        t_enddatetime=t_enddatetime.addSeconds(59);
        
        
        datetime Ls_startdatetime;
        datetime Ls_enddatetime;
        
        //t_startdatetime=Appointment.Start_Date__c.format();
        system.debug('t_startdatetime'+t_startdatetime);
        
        List<Appointment__c> a=[select id, name,Appointment_Type__c,Status__c,StartDateTime__c,EndDateTime__c,Duration__c, ap.Client_First_Name__c,patient__c,subject__c from Appointment__c ap where   
         (
                                  (ap.StartDateTime__c >= :t_startdatetime  AND ap.StartDateTime__c <= :t_enddatetime) OR
                                  (ap.EndDateTime__c >= :t_startdatetime  AND ap.EndDateTime__c <= :t_enddatetime) OR
                                  (ap.StartDateTime__c <= :t_startdatetime   AND ap.EndDateTime__c >= :t_enddatetime)
                               )];
                               
        List<Event> eve=[select id, subject,StartDateTime,EndDateTime from event eve where   
         (
                                  (eve.StartDateTime >= :t_startdatetime  AND eve.StartDateTime <= :t_enddatetime) OR
                                  (eve.EndDateTime >= :t_startdatetime  AND eve.EndDateTime <= :t_enddatetime) OR
                                  (eve.StartDateTime <= :t_startdatetime   AND eve.EndDateTime >= :t_enddatetime)
                               )];
        
        
        /*Start_Date__c=: Appointment.Start_Date__c order by Start_Date__c asc ] ;*/
        System.debug(eve);
        
        string tshour;
        string tmin;
        string strLs_startdatetime;
        Integer noofslots=0;
        time overLapSlotsTime;
        Integer noofEslots=0;
        time overLapESlotsTime;
        string timeAMPM;
        integer timeHour;
        string timeMin;
        Decimal datediff=0;
        
 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */       
 /*=========================ERRORS OUT IN THIS BLOCK==============================*/ 
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ if (eve.size()>0) { for(integer z=0;z<eve.size();z++) { if(mapETimeToSlot.get(string.valueof(eve[z].StartDateTime.time())) != null) { // system.debug('+++++++++++++++++++++++++++++++++++++duration'+a[z].Duration__c); overLapESlotsTime=eve[z].StartDateTime.time(); datediff=((Decimal.valueOf(eve[z].EndDateTime.getTime()) - Decimal.valueOf(eve[z].StartDateTime.getTime()))/(1000*60*60))*60; system.debug('datediff '+datediff); if(datediff>30) { noofEslots=integer.valueOf((datediff/30)); system.debug('+++++++++++++++++++++++++++++++++++++noofslots'+noofEslots); for(Integer nctr=0;nctr<noofEslots;nctr++) { if(mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEvent!=null)
//-------------------------------------------------------------------------------------------
//////////////////////////////////////ERRORS ON ABOVE LINE//////////////////////////////////
//-------------------------------------------------------------------------------------------
 { mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEventOverlap = eve[z]; mapETimeToSlot.get(string.valueof(overLapESlotsTime)).status='Y'; system.debug('++++++++++++++++++++++++++++++++++++inside overlap'); } else { mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEvent = eve[z]; system.debug('+++++++++++++++++++++++++++++++++outside overlap'); } overLapESlotsTime=overLapESlotsTime.addMinutes(30); } //mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointment = a[z]; }else { if(mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEvent!=null) { mapETimeToSlot.get(string.valueof(eve[z].StartDateTime.time())).sEventOverlap = eve[z]; } else { mapETimeToSlot.get(string.valueof(eve[z].StartDateTime.time())).sEvent = eve[z]; } } } } } /*=========================================================================================*/
/********************************************THIS BLOCK BELOW WORKS********************************/
/*===========================================================================================*/


if (a.size()>0) { for(integer z=0;z<a.size();z++) { if(mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())) != null) { system.debug('+++++++++++++++++++++++++++++++++++++duration'+a[z].Duration__c); overLapSlotsTime=a[z].StartDateTime__c.time(); datediff=((Decimal.valueOf(a[z].EndDateTime__c.getTime()) - Decimal.valueOf(a[z].StartDateTime__c.getTime()))/(1000*60*60))*60; system.debug('datediff '+datediff); if(datediff>30) { noofslots=integer.valueOf((datediff/30)); system.debug('+++++++++++++++++++++++++++++++++++++noofslots'+noofslots); for(Integer nctr=0;nctr<noofslots;nctr++) { system.debug('+++++++++++++++++++++++++++++++++++++overLapSlotsTime'+overLapSlotsTime); if(mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointment!=null) { mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointmentOverlap = a[z]; mapTimeToSlot.get(string.valueof(overLapSlotsTime)).status='Y'; system.debug('++++++++++++++++++++++++++++++++++++inside overlap'); } else { mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointment = a[z]; system.debug('+++++++++++++++++++++++++++++++++outside overlap'); } overLapSlotsTime=overLapSlotsTime.addMinutes(30); } //mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointment = a[z]; }else { if(mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointment!=null) { mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointmentOverlap = a[z]; } else { mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointment = a[z]; } } } } } /*============================================================================================== for(integer lictr=0;lictr<liETimeSlots.size();lictr++) { system.debug('inside transfer of liETimeSlots to Formated List'); timeAMPM=liETimeSlots[lictr].tstart1.substring(0,5); timeHour=integer.valueof(timeAMPM.substring(0,2)); timeMin=timeAMPM.substring(2,5); system.debug('time hour value'+timeHour); system.debug('time hour value'+timeMin); if (timeHour>12) { timeHour=timehour-12; system.debug('timeHour++++++++++++++++++++++++++'+timeHour) ; timeAMPM=string.valueof(timeHour); if (timeAMPM.length()==1) { timeAMPM='0'+timeAMPM+timeMin+' PM'; } else { timeAMPM=timeAMPM+timeMin+' PM'; } } else { timeAMPM=string.valueof(timeHour); if (timeAMPM.length()==1) { timeAMPM='0'+timeAMPM+timeMin+' AM'; } else { timeAMPM=timeAMPM+timeMin+' AM'; } } liETimeSlots[lictr].tstart1=timeAMPM; //liTimeSlots_form[lictr].sAppointment=liTimeSlots[lictr].sAppointment; } system.debug('liETimeSlots++++++++++++++++++++++++'+liETimeSlots); /*============================================================================================*/ for(integer lictr=0;lictr<liTimeSlots.size();lictr++) { system.debug('inside transfer of liTimeSlots to Formated List'); timeAMPM=liTimeSlots[lictr].tstart1.substring(0,5); timeHour=integer.valueof(timeAMPM.substring(0,2)); timeMin=timeAMPM.substring(2,5); system.debug('time hour value'+timeHour); system.debug('time hour value'+timeMin); if (timeHour>12) { timeHour=timehour-12; system.debug('timeHour++++++++++++++++++++++++++'+timeHour) ; timeAMPM=string.valueof(timeHour); if (timeAMPM.length()==1) { timeAMPM='0'+timeAMPM+timeMin+' PM'; } else { timeAMPM=timeAMPM+timeMin+' PM'; } } else { timeAMPM=string.valueof(timeHour); if (timeAMPM.length()==1) { timeAMPM='0'+timeAMPM+timeMin+' AM'; } else { timeAMPM=timeAMPM+timeMin+' AM'; } } liTimeSlots[lictr].tstart1=timeAMPM; //liTimeSlots_form[lictr].sAppointment=liTimeSlots[lictr].sAppointment; } } } }

 

public with sharing class CEventTimeSlot {
	

	
	// public Time           tStart         {get; set;}
	public string status {get;set;}
	 public String tStart1 {get; set;}
    public Event sEvent {get; set;}
    public Event sEventOverlap {get; set;}

    public CEventTimeSlot(String startTime)
    {
        tStart1 = startTime;
        status='';
        sEvent = null;
        sEventOverlap=null;
    }



}

 

public with sharing class CTimeSlot {
	
	// public Time           tStart         {get; set;}
	public string status {get;set;}
	 public String tStart1 {get; set;}
    public Appointment__c sAppointment {get; set;}
    public Appointment__c sAppointmentOverlap {get; set;}

    public CTimeSlot(String startTime)
    {
        tStart1 = startTime;
        status='';
        sAppointment = null;
        sAppointmentOverlap=null;
    }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfcksfck

Perhaps this

 

 

mapETimeToSlot.get(string.valueof(overLapESlotsTime))

is returning null

 

 

 

All Answers

sfcksfck

When you create the list called eve, are you sure that it is not coming out null? 

 

If you reply to this please provide line numbers or at least highlight the lines that are mentioned in the error message

 

Thanks

 

 

 

pradyprady

Hi,

 

I have marked in the code exactly where the error is occuring.

It all works perfectly fine in sandbox, Its only when i am moving to production i get this problem.

The issue occurs in the Line where i am checking for null

 if(mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEvent!=null)

 

 

pradyprady

Here is code which is causing the issue...

 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */       
 /*=========================ERRORS OUT IN THIS BLOCK==============================*/ 
 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 
       if (eve.size()>0)
      {
        
            for(integer z=0;z<eve.size();z++)
            {
            if(mapETimeToSlot.get(string.valueof(eve[z].StartDateTime.time())) != null)
            {
               // system.debug('+++++++++++++++++++++++++++++++++++++duration'+a[z].Duration__c);
                overLapESlotsTime=eve[z].StartDateTime.time();
                
                datediff=((Decimal.valueOf(eve[z].EndDateTime.getTime()) - Decimal.valueOf(eve[z].StartDateTime.getTime()))/(1000*60*60))*60;
                system.debug('datediff    '+datediff);
                
                if(datediff>30)
                {
                    noofEslots=integer.valueOf((datediff/30));
                    system.debug('+++++++++++++++++++++++++++++++++++++noofslots'+noofEslots);
                    for(Integer nctr=0;nctr<noofEslots;nctr++)
                    {
                        
               if(mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEvent!=null) 
//-------------------------------------------------------------------------------------------
//////////////////////////////////////ERRORS ON ABOVE LINE//////////////////////////////////
//-------------------------------------------------------------------------------------------
 {
                        mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEventOverlap = eve[z];
                        mapETimeToSlot.get(string.valueof(overLapESlotsTime)).status='Y';
                        system.debug('++++++++++++++++++++++++++++++++++++inside overlap');
                        }
                        else
                        {
                            mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEvent = eve[z];
                            system.debug('+++++++++++++++++++++++++++++++++outside overlap');
                        }
                        overLapESlotsTime=overLapESlotsTime.addMinutes(30);   

                        
                    }
                    //mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointment = a[z];
                    
                    
                    
                }else
                {
                    if(mapETimeToSlot.get(string.valueof(overLapESlotsTime)).sEvent!=null)
                    {
                    mapETimeToSlot.get(string.valueof(eve[z].StartDateTime.time())).sEventOverlap = eve[z];
                    }
                    else
                    {
                        mapETimeToSlot.get(string.valueof(eve[z].StartDateTime.time())).sEvent = eve[z];
                    }
                    
                }  
                
            }   
            }
     
      }
 
 /*=========================================================================================*/
/********************************************THIS BLOCK BELOW WORKS********************************/
/*===========================================================================================*/
 


        
        
      if (a.size()>0)
      {
        
            for(integer z=0;z<a.size();z++)
            {
            if(mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())) != null)
            {
                system.debug('+++++++++++++++++++++++++++++++++++++duration'+a[z].Duration__c);
                overLapSlotsTime=a[z].StartDateTime__c.time();
                
                datediff=((Decimal.valueOf(a[z].EndDateTime__c.getTime()) - Decimal.valueOf(a[z].StartDateTime__c.getTime()))/(1000*60*60))*60;
                system.debug('datediff    '+datediff);
                
                if(datediff>30)
                {
                    noofslots=integer.valueOf((datediff/30));
                    system.debug('+++++++++++++++++++++++++++++++++++++noofslots'+noofslots);
                    for(Integer nctr=0;nctr<noofslots;nctr++)
                    {
                        system.debug('+++++++++++++++++++++++++++++++++++++overLapSlotsTime'+overLapSlotsTime);
                        if(mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointment!=null)
                        {
                        mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointmentOverlap = a[z];
                        mapTimeToSlot.get(string.valueof(overLapSlotsTime)).status='Y';
                        system.debug('++++++++++++++++++++++++++++++++++++inside overlap');
                        }
                        else
                        {
                            mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointment = a[z];
                            system.debug('+++++++++++++++++++++++++++++++++outside overlap');
                        }
                        overLapSlotsTime=overLapSlotsTime.addMinutes(30);   

                        
                    }
                    //mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointment = a[z];
                    
                    
                    
                }else
                {
                    if(mapTimeToSlot.get(string.valueof(overLapSlotsTime)).sAppointment!=null)
                    {
                    mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointmentOverlap = a[z];
                    }
                    else
                    {
                        mapTimeToSlot.get(string.valueof(a[z].StartDateTime__c.time())).sAppointment = a[z];
                    }
                    
                }
                
                
            }   
            }

           
            
      }
        

 

sfcksfck

Perhaps this

 

 

mapETimeToSlot.get(string.valueof(overLapESlotsTime))

is returning null

 

 

 

This was selected as the best answer
pradyprady

Would that happen coz i am checking for it in a if statement above

 

  if(mapETimeToSlot.get(string.valueof(eve[z].StartDateTime.time())) != null)
            {
               // system.debug('+++++++++++++++++++++++++++++++++++++duration'+a[z].Duration__c);
                overLapESlotsTime=eve[z].StartDateTime.time();
                

 

pradyprady

I have a very similar peice of code below the erroring code which works perfectly.

pradyprady

Hi,

 

Thanks for your pointer... The issue really was with the

mapETimeToSlot.get(string.valueof(overLapESlotsTime))

 I was checking for times falling with in 7AM to 10 PM with intervals of 30 mins. There were some events which were falling between 30 min interval like 7:28AM. So this waqs giving a null value which caused the error