I have a class that creates a Opportunity Product and sets a start date and end date. We have this idea of renewal where we clone an Opportunity and push the dates out to the future.

The way the calculations work now is a formula field takes end date - start date gets the number of days adds them to the end date and subtract the days to get to the first of the month.

This sort of works as we get the correct date most of the time, however it doesn't always work. So I want to do the easier method of .addmonths(Integer.Field__c

In the class I have these lines:

//set the current month/year and end month/year
        strCurrentMonth = String.valueOf(Datetime.now().month());
        intCurrentYear = Datetime.now().year();
        if(objOpportunity.ContractTerm__c != null)
          strEndMonth = String.valueOf(Datetime.now().addMonths(Integer.valueOf(objOpportunity.ContractTerm__c) - 1).month());
          intEndYear = Datetime.now().addMonths(Integer.valueOf(objOpportunity.ContractTerm__c) - 1).Year();

objWarpper.objOppLineItem.Start_Date__c = objWarpper.intCurrentYear == null || objWarpper.intCurrentYear == 0 ? null :
       Date.newInstance(objWarpper.intCurrentYear, Integer.valueOf(objWarpper.strCurrentMonth), 1);
objWarpper.objOppLineItem.End_Date__c = objWarpper.intEndYear == null || objWarpper.intEndYear == 0 ?  null :
       Date.newInstance(objWarpper.intEndYear, Integer.valueOf(objWarpper.strEndMonth), 1);

I want my renewal fields to be populated when the record is created so I don't have to calculate this all again. Thus, can I just do the same logic as the start and end date and add more months to it? Like this, but it doesn't seem to work as I get the error System.NullPointerException: Attempt to de-reference a null object when the record tries to be created.

objWarpper.objOppLineItem.Start_Date_Renewal__c = objWarpper.intCurrentYear == null || objWarpper.intCurrentYear == 0 ? null :
       (Date.newInstance(objWarpper.intCurrentYear, Integer.valueOf(objWarpper.strCurrentMonth), 1)).addMonths(Integer.valueOf(objWarpper.objOppLineItem.Opportunity.ContractTerm__c) - 1 );
objWarpper.objOppLineItem.End_Date_Renewal__c = objWarpper.intEndYear == null || objWarpper.intEndYear == 0 ?  null :
       (Date.newInstance(objWarpper.intEndYear, Integer.valueOf(objWarpper.strEndMonth), 1)).addMonths(Integer.valueOf(objWarpper.objOppLineItem.Opportunity.ContractTerm__c) - 1 );
  • in order to properly help you, it would help if you could find out which exact variable is being null. So add some system.debug() statements with the variables in the line of code that is throwing the exception. Aug 5, 2016 at 19:46
  • now would be a good time to fix the spelling of objWarpper :-) You can also make you code easier to read by renaming objOppLineItem to oli, objOpportunity to o - why? these are implicitly objects so prefixing by obj just adds to confusion. In general, you are prefixing every variable with its type which if you note in virtually every Stackexchange post and every SFDC example is never done
    – cropredy
    Aug 7, 2016 at 19:47
  • That's a good point. I didn't create the original code. But I suppose as long as I'm there I should make it nice. Aug 7, 2016 at 20:08
  • Figured it out, the wrapper vs warpper got me. Sublime was trying to do me a solid and auto complete and I didn't see the word difference. Hence why it said null reference because the reference was empty. I fixed and bam works. Aug 10, 2016 at 20:47

1 Answer 1


When reviewing code created by someone else be sure to look closely at variable names. Sometimes the developer will spell a word wrong and then create a new variable with the correct name.

In this example objWarpper was used and objWrapper was used in a different place...

If you use the wrong variable in the wrong place the code won't work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .