+ Start a Discussion
Andy Kallio 7Andy Kallio 7 

Admin can't initialize variable!

Hi All. My test code is giving a null pointer exception on line 18, and I don't understand why. I'm sure that there is something basic about intializing variables that I'm not understanding. I thought that intializing the variable to 0 would get me around the null pointer exception on line 18, but it's not. 
 
public class createBillingSchedule {
    
    @InvocableMethod
    public static void createBillingSchedule(List<String> PrjIds) {
        Date firstDay = date.today();
        Date lastDay = date.today();
       	Date lastDayOfMonth = date.today();
       	Integer totalDaysInMonth = 0;
       	Integer remainingDaysInMonth = 0;  
        Integer i = 0;
        List<Billing_Schedule__c> bsList = new List<Billing_Schedule__c>();
       
        for(Project__c prj : [select Id, Name, Start_Date__c, End_Date__c, Budget__c, Daily_Budget__c from Project__c where ID IN :PrjIds]) {
                      
            do {
            	i++;
                if(i == 1) { 
                    totalDaysInMonth = Date.daysInMonth(prj.Start_Date__c.year(), prj.Start_Date__c.month());
					lastDayOfMonth = Date.newInstance(prj.Start_Date__c.year(), prj.Start_date__c.month(), totalDaysInMonth);
                    remainingDaysInMonth = prj.Start_Date__c.daysBetween(lastDayOfMonth);
                    firstDay = Date.newInstance(prj.Start_Date__c.year(), prj.Start_date__c.month(), totalDaysInMonth)+1;
                    Billing_Schedule__c bs1 = new Billing_Schedule__c(
                    	Project__c = prj.Id,
                        Requested_Amount__c = remainingDaysInMonth * prj.Daily_Budget__c,
                        Billing_Date__c = lastDayOfMonth,
                        Status__c = 'Waiting to Invoice'
                    );
                    bsList.add(bs1);
                } else if (i > 1 && i < prj.Duration_Months__c) {
                    totalDaysInMonth = Date.daysInMonth(firstDay.year(), firstDay.month());
					lastDayOfMonth = Date.newInstance(firstDay.year(), firstDay.month(), totalDaysInMonth);
                    remainingDaysInMonth = firstDay.daysBetween(lastDayOfMonth);
                    firstDay = Date.newInstance(firstDay.year(), firstDay.month(), totalDaysInMonth)+1;
                    Billing_Schedule__c bs2 = new Billing_Schedule__c(
                    	Project__c = prj.Id,
                        Requested_Amount__c = remainingDaysInMonth * prj.Daily_Budget__c,
                        Billing_Date__c = lastDayOfMonth,
                        Status__c = 'Waiting to Invoice'
                    );
                    bsList.add(bs2);
                } else if (i == prj.Duration_Months__c) {
                    totalDaysInMonth = Date.daysInMonth(prj.End_date__c.year(), prj.End_date__c.month());
					lastDayOfMonth = Date.newInstance(prj.End_date__c.year(), prj.End_date__c.month(), totalDaysInMonth);
                    remainingDaysInMonth = firstDay.daysBetween(prj.End_date__c);
                    Billing_Schedule__c bs3 = new Billing_Schedule__c(
                    	Project__c = prj.Id,
                        Requested_Amount__c = remainingDaysInMonth * prj.Daily_Budget__c,
                        Billing_Date__c = lastDayOfMonth,
                        Status__c = 'Waiting to Invoice'
                    );
                    bsList.add(bs3);
                }
            }
            while (i <= prj.Duration_Months__c);
        }
        insert bsList;
    }

}

 
Best Answer chosen by Andy Kallio 7
Martijn SchwarzerMartijn Schwarzer
Hi Andy,

The nullpointer is probably because the field Start_Date__c is not filled. If you then try to get the year from the date, you will get a nullpointer error.

Did you fill the field when you created your test code?

Best regards,
Martijn Schwärzer

All Answers

Martijn SchwarzerMartijn Schwarzer
Hi Andy,

The nullpointer is probably because the field Start_Date__c is not filled. If you then try to get the year from the date, you will get a nullpointer error.

Did you fill the field when you created your test code?

Best regards,
Martijn Schwärzer
This was selected as the best answer
Andy Kallio 7Andy Kallio 7
Thanks Martin! That was the issue...onto other issues now...I will have a go at them, but may post here again.