+ Start a Discussion
Amit Yadav 9Amit Yadav 9 

Attempt to de-reference a null object 

 public void fun()
   {  
       list<Payment__c> pay=new list<Payment__c>();
       for(Payment__c nop:[select id,,npe01__Scheduled_Date__c, from Payment__c where Paid__c=true])
       {
           integer year=nop.npe01__Scheduled_Date__c.year();
           integer month=nop.npe01__Scheduled_Date__c.month();
         
     }

when i run this code in developer console in production it gives error:
System.NullPointerException: Attempt to de-reference a null object 
   on the highlighted part.
This is working fine in sandbox. Deployed without any problems.
Checked the query in query editor. It is returning records as expected.
What could be the problem here???
Any help is appreciated.
Thank you
Arunkumar RArunkumar R
Hi Amit,

You have to add null condition in your query or before processing the logic,

Way 1:
public void fun()
   {  
       list<Payment__c> pay=new list<Payment__c>();
       for(Payment__c nop:[select id,,npe01__Scheduled_Date__c, from Payment__c where Paid__c=true AND npe01__Scheduled_Date__c != null])
       {
           integer year=nop.npe01__Scheduled_Date__c.year();
           integer month=nop.npe01__Scheduled_Date__c.month();
         
       }
}

Way 2:
public void fun()
   {  
       list<Payment__c> pay=new list<Payment__c>();
       for(Payment__c nop:[select id,,npe01__Scheduled_Date__c, from Payment__c where Paid__c=true])
       {
           if(nop.npe01__Scheduled_Date__c != null)
           {
               integer year=nop.npe01__Scheduled_Date__c.year();
               integer month=nop.npe01__Scheduled_Date__c.month();
           }
       }
         
  }