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
Holly Havelka 10Holly Havelka 10 

Help with Test Class System.NullPointerException: Argument cannot be null.

Hi all,

I am struggling with figuring out why my last test keeps failing.  Wondering if a second pair of eyes can pinpoint why the 'Application' is not being created in the test class.

Here is my test class (only showing the last test due to size restrictions):
static testMethod void newApplicationTest() {
        Breakthrough_Application_Settings__c setting = new Breakthrough_Application_Settings__c();
        setting.Name = '2015';
        setting.Application_Closed__c = Date.valueOf(Datetime.now().addDays(20));
        setting.Backdoor_Application_Open__c = Date.today();
        setting.Active__c = true;
        insert setting;
        Account a = AccountUtils.createBreakthroughOrgAccount('Breakthrough Alumni Test', true);
        Contact c = ContactUtils.createAlumniContact('MyNew', 'Alumnus1', 'myalumnus1@org.test', a.Id, true);
        
        User u = UserUtils.createAlumniCommunityUser(c, false, true);
        
        PageReference ref = new PageReference('/apex/ApplicationStatus');
        Test.setCurrentPage(ref);
        ApplicationStatusController controller = new ApplicationStatusController();
        Test.startTest();
            System.runAs(u) {
                ref = controller.newApplication();
            }
        Test.stopTest();
        system.assert(controller.application.Id != null);
    }
Here is my controller:
public class ApplicationStatusController {
  
  public List<ReviewStatus> reviewStatusList { get; set; }
  public Application__c application { get; set; }
  public Recommendation_N__c recommendation { get; set; }
  public Breakthrough_Application_Settings__c settings { get; set; }
  public User user { get; set; }
  public Boolean applicationClosed { get; set; }
  public String recommendationDate { get; set; }
  public String whenSubmitted { get; set; }
  private Map<String,String> citiesMap {get;set;}
  public Boolean backdoorApplicationOpen { get; set; }
  public Boolean isRegularDeadlineReach { get; set; }
  public Boolean isDisplayed { get; set; }
  
  public ApplicationStatusController() {
    this.settings = [select Regular_Deadline__c,
                  Early_Action_Deadline__c, 
                Early_Action_Rollover__c, 
                First_Rollover__c, 
                Second_Rollover__c, 
                Third_Rollover__c,
                Application_Closed__c,
                Backdoor_Application_Open__c,
                Backdoor_Application_Closed__c,
                Display__c,
                Name 
              from Breakthrough_Application_Settings__c 
              where Active__c = true LIMIT 1];
    Datetime regularDeadline = Datetime.newInstance(this.settings.Regular_Deadline__c, Time.newInstance(0,0,0,0)).addDays(1);
    this.applicationClosed = settings.Application_Closed__c <= Date.today();
    this.isDisplayed = settings.Display__c;
    this.isRegularDeadlineReach = Datetime.now() >= regularDeadline;
    Datetime backdoorOpen = Datetime.newInstance(this.settings.Backdoor_Application_Open__c, Time.newInstance(0,0,0,0));
    this.backdoorApplicationOpen = Datetime.now() >= backdoorOpen;
    this.loadApplication();
  }

  private void loadApplication() {
    this.user = [select ContactId from User where Id =: UserInfo.getUserId()];
    
    List<Application__c> applicationList = [select Application_Year__c, 
                            AppStatus__c, 
                            Contact__c,
                            Page_1_Status__c,
                            Page_2_Status__c,
                            Page_3_Status__c,
                            Page_4_Status__c,
                            Page_5_Status__c,
                            X1st_City_Preference__c,
                            X1st_City_Preference__r.Name,
                            X1st_City_Preference__r.Early_Decision_Participant__c,
                            X2nd_City_Preference__c,
                            X2nd_City_Preference__r.Name,
                            X2nd_City_Preference__r.Early_Decision_Participant__c,
                            X3rd_City_Preference__c,
                            X3rd_City_Preference__r.Name,
                            X3rd_City_Preference__r.Early_Decision_Participant__c,
                            Mock_Teaching_Video_Link__c,
                            Ask_for_Mock_Teaching_Video__c,
                            WhenSubmitted__c,
                            Frozen__c,
                            Early_Rollover_Done__c,
                            First_Rollover_Done__c,
                            Second_Rollover_Done__c,
                            Third_Rollover_Done__c,
                            OwnerId,
                            RecordType.Name
                            from Application__c 
                            where Application_Year__c =: this.settings.Name
                            and Contact__c =: user.ContactId 
                            and RecordType.Name IN ('Backdoor', 'Portal Application')
                            LIMIT 1];
    if (!applicationList.isEmpty()) {
      this.application = applicationList[0];
      this.whenSubmitted = this.application.WhenSubmitted__c == null ? '' : this.application.WhenSubmitted__c.format('EEEE, MMMM d, yyyy \'at\' h:mma');
      this.loadRecommendation();
      this.loadCitiesMap();
      this.loadReviewStatus();
    }
  }
  
  private void loadReviewStatus() {
    Datetime earlyDeadline = Datetime.newInstance(this.settings.Early_Action_Deadline__c, Time.newInstance(0,0,0,0)).addDays(1);
    
    Map<String, String> appStatusToReviewStatusMap = this.loadStatusMap();
    
    this.reviewStatusList = new List<ReviewStatus>();
    if (this.application.RecordType.Name == 'Backdoor') {
      this.loadReviewStatusForBackdoor(appStatusToReviewStatusMap);
    } else {
      if (!String.isEmpty(this.application.X1st_City_Preference__c)) {
        ReviewStatus review = new ReviewStatus();
        review.preference = this.application.X1st_City_Preference__r.Name;
        if (this.application.X1st_City_Preference__r.Early_Decision_Participant__c == 'Yes' && this.application.WhenSubmitted__c <= earlyDeadline) {
          review.reviewDeadline = this.settings.Early_Action_Rollover__c.month() + '/' + (this.settings.Early_Action_Rollover__c.day()+1) + '/' + this.settings.Early_Action_Rollover__c.year();
        } else {
          review.reviewDeadline = this.settings.First_Rollover__c.month() + '/' + (this.settings.First_Rollover__c.day()+1) + '/' + this.settings.First_Rollover__c.year();
        }
        if (this.application.AppStatus__c == 'Wait Listed' || this.application.AppStatus__c == 'Waitlisted/No Offer Extended' || this.application.AppStatus__c == 'No Offer Extended') {
          review.status = 'No Offer Extended';
        } else {
          if (this.application.Early_Rollover_Done__c && this.application.X1st_City_Preference__r.Early_Decision_Participant__c == 'Yes' && this.application.WhenSubmitted__c <= earlyDeadline) {
            review.status = 'No Offer Extended';
          } else if (this.application.First_Rollover_Done__c) {
            review.status = 'No Offer Extended';
          } else {
            if (this.currentCityIndex()==0) {
              
              review.status = appStatusToReviewStatusMap.containsKey(this.application.AppStatus__c) ? appStatusToReviewStatusMap.get(this.application.AppStatus__c) : '';
                
            } else if(this.currentCityIndex()>0){
              
              if (this.application.AppStatus__c == 'Hold / Keep' || this.application.AppStatus__c == 'Assigned') 
                review.status = 'No Offer Extended';
              if (this.application.AppStatus__c == 'Offer Pending' || this.application.AppStatus__c == 'Offer Accepted' || this.application.AppStatus__c == 'Offer Rejected' || this.application.AppStatus__c == 'Rejected' || this.application.AppStatus__c == 'Applicant Dismissed' || this.application.AppStatus__c == 'Applicant Has Withdrawn' || this.application.AppStatus__c == 'Teaching Fellow Dismissed' || this.application.AppStatus__c == 'Teaching Fellow Withdrawn')
                review.status = 'No Offer Extended';
                
            }
          }
          
        }
        
        reviewStatusList.add(review);
      }
      if (!String.isEmpty(this.application.X2nd_City_Preference__c)) {
        ReviewStatus review = new ReviewStatus();
        review.preference = this.application.X2nd_City_Preference__r.Name;
        if (this.application.X1st_City_Preference__r.Early_Decision_Participant__c == 'Yes' && this.application.WhenSubmitted__c <= earlyDeadline) {
          review.reviewDeadline = this.settings.First_Rollover__c.month() + '/' + (this.settings.First_Rollover__c.day()+1) + '/' + this.settings.First_Rollover__c.year();
        } else {
          review.reviewDeadline = this.settings.Second_Rollover__c.month() + '/' + (this.settings.Second_Rollover__c.day()+1) + '/' + this.settings.Second_Rollover__c.year();
        }
        if (this.application.AppStatus__c == 'Wait Listed' || this.application.AppStatus__c == 'Waitlisted/No Offer Extended' || this.application.AppStatus__c == 'No Offer Extended') {
          review.status = 'No Offer Extended';
        } else {
          if (this.application.Second_Rollover_Done__c) {
            review.status = 'No Offer Extended';
          } else {
            if (this.currentCityIndex()==1) {
  
              review.status = appStatusToReviewStatusMap.containsKey(this.application.AppStatus__c) ? appStatusToReviewStatusMap.get(this.application.AppStatus__c) : '';
                
            } else if (this.currentCityIndex()>1){
              
              if (this.application.AppStatus__c == 'Hold / Keep' || this.application.AppStatus__c == 'Assigned') 
                review.status = 'No Offer Extended';
              if (this.application.AppStatus__c == 'Offer Pending' || this.application.AppStatus__c == 'Offer Accepted' || this.application.AppStatus__c == 'Offer Rejected' || this.application.AppStatus__c == 'Rejected' || this.application.AppStatus__c == 'Applicant Dismissed' || this.application.AppStatus__c == 'Applicant Has Withdrawn' || this.application.AppStatus__c == 'Teaching Fellow Dismissed' || this.application.AppStatus__c == 'Teaching Fellow Withdrawn')
                review.status = 'No Offer Extended';
                
            } else {
              
              if (this.application.AppStatus__c == 'Hold / Keep' || this.application.AppStatus__c == 'Assigned') 
                review.status = 'Awaiting Review';
              if (this.application.AppStatus__c == 'Offer Pending' || this.application.AppStatus__c == 'Offer Accepted' || this.application.AppStatus__c == 'Offer Rejected' || this.application.AppStatus__c == 'Rejected' || this.application.AppStatus__c == 'Applicant Dismissed' || this.application.AppStatus__c == 'Applicant Has Withdrawn' || this.application.AppStatus__c == 'Teaching Fellow Dismissed' || this.application.AppStatus__c == 'Teaching Fellow Withdrawn')
                review.status = '';
                
            }
          }
        }
        reviewStatusList.add(review);
      }
      if (!String.isEmpty(this.application.X3rd_City_Preference__c)) {
        ReviewStatus review = new ReviewStatus();
        review.preference = this.application.X3rd_City_Preference__r.Name;
        if (this.application.X1st_City_Preference__r.Early_Decision_Participant__c == 'Yes' && this.application.WhenSubmitted__c <= earlyDeadline) {
          review.reviewDeadline = this.settings.Second_Rollover__c.month() + '/' + (this.settings.Second_Rollover__c.day()+1) + '/' + this.settings.Second_Rollover__c.year();
        } else {
          review.reviewDeadline = this.settings.Third_Rollover__c.month() + '/' + (this.settings.Third_Rollover__c.day()+1) + '/' + this.settings.Third_Rollover__c.year();
        }
        if (this.application.AppStatus__c == 'Wait Listed' || this.application.AppStatus__c == 'Waitlisted/No Offer Extended'  || this.application.AppStatus__c == 'No Offer Extended') {
          review.status = 'No Offer Extended';
        } else {
          if (this.application.Third_Rollover_Done__c) {
            review.status = 'No Offer Extended';
          } else {
            if (this.currentCityIndex()==2) {
  
              review.status = appStatusToReviewStatusMap.containsKey(this.application.AppStatus__c) ? appStatusToReviewStatusMap.get(this.application.AppStatus__c) : '';
                
            }else if (this.currentCityIndex()>2) {  
              if (this.application.AppStatus__c == 'Hold / Keep' || this.application.AppStatus__c == 'Assigned') 
                review.status = 'No Offer Extended';
              if (this.application.AppStatus__c == 'Offer Pending' || this.application.AppStatus__c == 'Offer Accepted' || this.application.AppStatus__c == 'Offer Rejected' || this.application.AppStatus__c == 'Rejected' || this.application.AppStatus__c == 'Applicant Dismissed' || this.application.AppStatus__c == 'Applicant Has Withdrawn' || this.application.AppStatus__c == 'Teaching Fellow Dismissed' || this.application.AppStatus__c == 'Teaching Fellow Withdrawn')
                review.status = 'No Offer Extended';
                
            } else {
              
              if (this.application.AppStatus__c == 'Hold / Keep' || this.application.AppStatus__c == 'Assigned') 
                review.status = 'Awaiting Review';
              if (this.application.AppStatus__c == 'Offer Pending' || this.application.AppStatus__c == 'Offer Accepted' || this.application.AppStatus__c == 'Offer Rejected' || this.application.AppStatus__c == 'Rejected' || this.application.AppStatus__c == 'Applicant Dismissed' || this.application.AppStatus__c == 'Applicant Has Withdrawn' || this.application.AppStatus__c == 'Teaching Fellow Dismissed' || this.application.AppStatus__c == 'Teaching Fellow Withdrawn')
                review.status = '';
                
            }
          }
        }
        reviewStatusList.add(review);
      }
      if (this.application.AppStatus__c == 'Wait Listed' || this.application.AppStatus__c == 'Waitlisted/No Offer Extended') {
        ReviewStatus review = new ReviewStatus();
        review.preference = 'National Waitlist';
        review.reviewDeadline = this.settings.Application_Closed__c.month() + '/' + (this.settings.Application_Closed__c.day()+1) + '/' + this.settings.Application_Closed__c.year();
        review.status = '';
        reviewStatusList.add(review);
      }
    }
  }
  
  private void loadReviewStatusForBackdoor(Map<String, String> appStatusToReviewStatusMap) {
    ReviewStatus review = new ReviewStatus();
    if (this.application.AppStatus__c == 'Wait Listed' || this.application.AppStatus__c == 'Waitlisted/No Offer Extended') {
      review.preference = 'National Waitlist';
      review.status = '';
    } else if (this.currentCityIndex() == 0) {
      review.preference = this.application.X1st_City_Preference__r.Name;
      review.status = appStatusToReviewStatusMap.containsKey(this.application.AppStatus__c) ? appStatusToReviewStatusMap.get(this.application.AppStatus__c) : '';
    } else {
      review.preference = 'Rejected Queue';
      review.status = '';
    }
    review.reviewDeadline = this.settings.Backdoor_Application_Closed__c.month() + '/' + (this.settings.Backdoor_Application_Closed__c.day()+1) + '/' + this.settings.Backdoor_Application_Closed__c.year();
    this.reviewStatusList.add(review);
  }

  private void loadCitiesMap(){
    List<String> cities = new List<String>();

    cities.add(this.application.X1st_City_Preference__r.Name);
    cities.add(this.application.X2nd_City_Preference__r.Name);
    cities.add(this.application.X3rd_City_Preference__r.Name);

    List<Breakthrough_Application_City_Queue_Map__c> citiesMapList = [Select Name, City__c, QueueId__c FROM Breakthrough_Application_City_Queue_Map__c Where City__c IN :cities];

    this.citiesMap = new Map<String,String>();

    for(Breakthrough_Application_City_Queue_Map__c c : citiesMapList){
      this.citiesMap.put(c.QueueId__c , c.City__c );
    }
  }

  private Integer currentCityIndex(){
    String currentCity = this.citiesMap.get(String.valueOf(this.application.OwnerId).substring(0, 15));

    if(currentCity == this.application.X1st_City_Preference__r.Name){
      return 0;
    }
    if(currentCity == this.application.X2nd_City_Preference__r.Name){
      return 1;
    }
    if(currentCity == this.application.X3rd_City_Preference__r.Name){
      return 2;
    }
    return -1;
  }
  
  private void loadRecommendation() {
    List<Recommendation_N__c> recommendationList = [select Status__c,
                                Recommender__r.Name,
                                Recommender__r.Email,
                                Orig_Email_Sent_On__c
                            from Recommendation_N__c 
                            where Application__c =: this.application.Id
                            LIMIT 1];
    if (!recommendationList.isEmpty()) {
      this.recommendation = recommendationList[0];
      this.recommendationDate = this.recommendation.Orig_Email_Sent_On__c == null ? '' : this.recommendation.Orig_Email_Sent_On__c.format('EEEE, MMMM d, yyyy \'at\' h:mma');
    }                            
  }
  
  public PageReference submitVideo() {
    return new PageReference('/apex/TeachingVideoSubmissionPage');
  }
  
  public PageReference newApplication() {
    RecordType record = this.backdoorApplicationOpen ? [SELECT Id FROM RecordType WHERE Name = 'Backdoor'] : [SELECT Id FROM RecordType WHERE Name = 'Portal Application'];
    this.application = new Application__c();
    this.application.Application_Year__c = this.settings.Name;
    this.application.Contact__c = this.user.ContactId;
    this.application.AppStatus__c = 'Application In Progress';
    this.application.RecordTypeId = record.Id;
    this.application.When_Started__c = Datetime.now();
    try {
      insert this.application;  
    } catch(Exception e) {
      PageReference ref = new PageReference('/apex/ApplicationStatus');
      ref.setRedirect(true);
      return ref;
    }
    return null;
  }
  
  private Map<String, String> loadStatusMap() {
    Map<String, String> statusMap = new Map<String, String>();
    statusMap.put('Hold / Keep', 'Under Review');
    statusMap.put('Assigned', 'Under Review');
    statusMap.put('Offer Pending', 'Offer Pending');
    statusMap.put('Offer Accepted', 'Offer Accepted');
    statusMap.put('Offer Rejected', 'Offer Rejected');
    statusMap.put('Rejected', 'Offer Rejected');
    statusMap.put('Applicant Dismissed', 'No Offer Extended');
    statusMap.put('Applicant Has Withdrawn', 'Application Withdrawn');
    statusMap.put('Teaching Fellow Dismissed', 'Teaching Fellow Dismissed');
    statusMap.put('Teaching Fellow Withdrawn', 'Teaching Fellow Withdrawn');
    return statusMap;
  }
  
  public class ReviewStatus {
    public String preference { get; set; }
    public String status { get; set; }
    public String reviewDeadline { get; set; }
  }
  
}


 
Best Answer chosen by Holly Havelka 10
Abhishek BansalAbhishek Bansal
Hi Holly,

In your original question you mentioned that there is Argument cannot be null error. I think that has been resolved.
Reagrding the assertion failed, yes, you are right there is no application record being inserted in your test case. In order to get rid of this either insert the appication record or change your assert as per the test class.

Thanks,
Abhishek Bansal.

All Answers

Paul S.Paul S.
Holly - what line of code does the error reference?
Holly Havelka 10Holly Havelka 10
Hi Paul,

It references Line 30 column 1 of the controller code.  The error is thrown on line 15 column 1 of the test class.
Paul S.Paul S.
Check your "settings" query.  It looks like it's returning null, which then has you passing a null to your datetime.newInstance method.
Paul S.Paul S.
Oh - I don't think your test class is setting the value of regular_deadline__c
Deepak Pandey 13Deepak Pandey 13
Before line 30 , you just put a condition if(this.settings.Regular_Deadline__c != null ), Becouse when the test  class execute it's return null.
Abhishek BansalAbhishek Bansal
Hi Holy,

You should add the value in the Regular_Deadline__c field in your test class as mentioned below:
static testMethod void newApplicationTest() {
	Breakthrough_Application_Settings__c setting = new Breakthrough_Application_Settings__c();
	setting.Name = '2015';
	setting.Application_Closed__c = Date.valueOf(Datetime.now().addDays(20));
	setting.Backdoor_Application_Open__c = Date.today();
	setting.Active__c = true;
	
	//Add value as per the data type of the field
	setting.Regular_Deadline__c = Date.today();
	
	insert setting;
	Account a = AccountUtils.createBreakthroughOrgAccount('Breakthrough Alumni Test', true);
	Contact c = ContactUtils.createAlumniContact('MyNew', 'Alumnus1', 'myalumnus1@org.test', a.Id, true);
	
	User u = UserUtils.createAlumniCommunityUser(c, false, true);
	
	PageReference ref = new PageReference('/apex/ApplicationStatus');
	Test.setCurrentPage(ref);
	ApplicationStatusController controller = new ApplicationStatusController();
	Test.startTest();
		System.runAs(u) {
			ref = controller.newApplication();
		}
	Test.stopTest();
	system.assert(controller.application.Id != null);
}

Please let me know if you need any other help on this.

Thanks,
Abhishek Bansal.​
Holly Havelka 10Holly Havelka 10
Thanks everyone.  Even with all of your suggestions I am still getting the same error 'System.AssertException: Assertion Failed'
Holly Havelka 10Holly Havelka 10
Is it possible that the test application is not being created, which is why it's acting like the application.id is null?
Abhishek BansalAbhishek Bansal
Hi Holly,

In your original question you mentioned that there is Argument cannot be null error. I think that has been resolved.
Reagrding the assertion failed, yes, you are right there is no application record being inserted in your test case. In order to get rid of this either insert the appication record or change your assert as per the test class.

Thanks,
Abhishek Bansal.
This was selected as the best answer