+ Start a Discussion
daniel1110daniel1110 

System.NullPointerException pls help

trigger createOrderforABFSCB on Stem_Cell_Bank__c (after update) {

    List<ChargentOrders__ChargentOrder__c> co = new List<ChargentOrders__ChargentOrder__c>();	
   
	for (Stem_Cell_Bank__c scb : Trigger.new) {
		if (scb.Stage__c == 'Stem Cells Banked') { /*scb to oscb*/
			
			//initiate the object to put values for furture record
			ChargentOrders__ChargentOrder__c c = new ChargentOrders__ChargentOrder__c();

			//map Order fields to SCB that is being created for this Order
			c.Stem_Cell_Bank__c = scb.Id; /*scb to oscb*/

	        c.ChargentOrders__Date__c = date.today();
	        c.ChargentOrders__Gateway__c = 'xxxxxxxxxxxxxxxxx';
	        c.ChargentOrders__Currency__c = 'U.S. Dollar';
	        c.Order_Item__c = 'Yearly Subscription Fee';
                // add one year to stem cell banked date
                c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c.addYears(1);

	        
			//add this new object to the list that would be inserted later
			co.add(c);
		} //end if
	} //end for scb
	
	//once the loop is done, insert the new record to Order
	try{
		insert co;
	} catch (system.Dmlexception e) {
		system.debug(e);
	}

}

 

Hi, as you could see, i have a pretty straightforward trigger, but i am getting the following error:

System.NullPointerException: Attempt to de-reference a null object Trigger.createOrderforABFSCB

 

it appears to not like c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c.addYears(1);

 

Stem_Cells_Banked_Date__c is a DATE data type, but i can't seem to use the addYears() method

 

Any suggestion would be appreciated.

 

thank u

Best Answer chosen by Admin (Salesforce Developers) 
liron169liron169
If the field: Stem_Cells_Banked_Date__c
is null, then you can't use the method addYears on it.

You can solve it by:

if(scb.Stem_Cells_Banked_Date__c!=null)
c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c.addYears(1);
else
//What to do if Stem_Cells_Banked_Date__c is null???

All Answers

liron169liron169
If the field: Stem_Cells_Banked_Date__c
is null, then you can't use the method addYears on it.

You can solve it by:

if(scb.Stem_Cells_Banked_Date__c!=null)
c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c.addYears(1);
else
//What to do if Stem_Cells_Banked_Date__c is null???
This was selected as the best answer
Devender MDevender M
Hi,

Before adding year check the value is present of not.

if(scb.Stem_Cells_Banked_Date__c!=null)
c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c.addYears(1);
daniel1110daniel1110