+ Start a Discussion
Julianne TajubaJulianne Tajuba 

System.NullPointerException: Attempt to de-reference a null object and System.DmlException: Insert failed.

List of Apex Test Failures
So I've run into some issues that I've not seen before as I attempt to move an updated visualforce page and apex class out of the sandbox and into production. I'm not sure what the CTLR_VolunteerAppliationTest is referring to, so I'm not sure where to start to fix this problem. The second set of failures regarding System.DmlException: Insert failed. don't make sense as the class names listed: Paypal_Autherization and below aren't even included anywhere on the visualforce page or apex class I am attempting to move. Any pointers for a newbie to apex testing and visualforce development would be greatly appreciated.

For Reference Apex Class Code:

public with sharing class CTLR_VolunteerApplication_Updates {
    public final ApexPages.StandardController controller;
    public Account account { get; set;}
    
    public CTLR_VolunteerApplication_Updates(ApexPages.StandardController controller) {
        this.controller = controller;
        this.account = (Account)controller.getRecord();
        
    }
      
    public String salutation {get; set;}
    public String fname {get; set;}
    public String lname {get; set;}  
    
    //get the multi-select pick list values
    public List<SelectOption> MPOptions {
     get {
       List<SelectOption> options = new List<SelectOption>();
       
       DescribeFieldResult volunteerAvail = Schema.Account.GW_Volunteers__Volunteer_Availability__pc.getDescribe();
         
       List<Schema.PicklistEntry> f = volunteerAvail.getPicklistValues();
       Integer listSize = f.size();
         
       for(Schema.PicklistEntry a : f) {
           options.add(new SelectOption(a.getValue(), a.getLabel()));
        } 
       return options;
     }  
     set;
    }
    
    //get and set the multi-select pick list as checkboxes
       public String[] MPItems { 
     get {
        String[] selected = new List<String>();
        List<SelectOption> sos = this.MPOptions;
        for(SelectOption s : sos) {
        if (this.account.GW_Volunteers__Volunteer_Availability__pc !=null && this.account.GW_Volunteers__Volunteer_Availability__pc.contains(s.getValue()))
           selected.add(s.getValue());
        }
        return selected;
     }public set {
        String selectedCheckBox = '';
        for(String s : value) {
         if (selectedCheckBox == '') 
           selectedCheckBox += s;
         else selectedCheckBox += ';' + s;
        }
        account.GW_Volunteers__Volunteer_Availability__pc = selectedCheckBox;
     }
   } 
    
    
    //get the multi-select pick list values
    public List<SelectOption> MPOptions2 {
     get {
       List<SelectOption> options = new List<SelectOption>();
       
       DescribeFieldResult volunteerInterests = Schema.Account.GW_Volunteers__Volunteer_Skills__pc.getDescribe();
         
       List<Schema.PicklistEntry> f = volunteerInterests.getPicklistValues();
       Integer listSize = f.size();
         
       for(Schema.PicklistEntry a : f) {
           options.add(new SelectOption(a.getValue(), a.getLabel()));
        } 
       return options;
     }  
     set;
    }
    
    //get and set the multi-select pick list as checkboxes
       public String[] MPItems2 { 
     get {
        String[] selected = new List<String>();
        List<SelectOption> sos = this.MPOptions;
        for(SelectOption s : sos) {
        if (this.account.GW_Volunteers__Volunteer_Skills__pc !=null && this.account.GW_Volunteers__Volunteer_Skills__pc.contains(s.getValue()))
           selected.add(s.getValue());
        }
        return selected;
     }public set {
        String selectedCheckBox = '';
        for(String s : value) {
         if (selectedCheckBox == '') 
           selectedCheckBox += s;
         else selectedCheckBox += ';' + s;
        }
        account.GW_Volunteers__Volunteer_Skills__pc = selectedCheckBox;
     }
   } 
    
//get the multi-select pick list values
    public List<SelectOption> MPOptions3 {
     get {
       List<SelectOption> options = new List<SelectOption>();
       
       DescribeFieldResult SpecialSkills = Schema.account.Special_Skills__c.getDescribe();  
     
       List<Schema.PicklistEntry> f = SpecialSkills.getPicklistValues();
       Integer listSize = f.size();
         
       for(Schema.PicklistEntry a : f) {
           options.add(new SelectOption(a.getValue(), a.getLabel()));
        } 
       return options;
     }  
     set;
    }

    
    //get and set the multi-select pick list as checkboxes
       public String[] MPItems3 { 
     get {
        String[] selected = new List<String>();
        List<SelectOption> sos = this.MPOptions;
        for(SelectOption s : sos) {
        if (this.account.Special_Skills__c !=null && this.account.Special_Skills__c.contains(s.getValue()))
           selected.add(s.getValue());
        }
        return selected;
     }public set {
        String selectedCheckBox = '';
        for(String s : value) {
         if (selectedCheckBox == '') 
           selectedCheckBox += s;
         else selectedCheckBox += ';' + s;
        }
        account.Special_Skills__c = selectedCheckBox;
     }
   } 
   
    public PageReference submit() {
      integer count = 0;
      count = [SELECT count() FROM Account WHERE FirstName = :account.FirstName AND LastName = :account.LastName ];
      System.debug('count' + count);
      
      if ( count == 0 )    {
      
          if (account.Terms_Conditions_Accepted__pc)  {
              try{
                    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
                    account.RecordType = personAccountRecordType;
                    account.Salutation = salutation;
                    account.FirstName = fname;
                    account.LastName = lname;
                    account.Contact_Email__c = account.PersonEmail;
                    account.Volunteer_Application_Submitted__c = true;
                    if ( [ SELECT count() FROM Account WHERE PersonEmail = :account.PersonEmail ] > 0 )
                      {
                       
                       Account a = [SELECT    Id,
                                              Salutation,
                                              FirstName,
                                              LastName,
                                              Contact_Email__c, 
                                              Volunteer_Application_Submitted__c,   
                                              PersonMailingStreet, 
                                              PersonMailingState,
                                              PersonMailingCity,
                                              PersonMailingPostalCode,
                                              PersonHomePhone,
                                              Phone,
                                              PersonEmail,
                                              Allergies_and_or_Restrictions__pc,
                                              How_did_you_hear_about_us__pc,
                                              Current_Occupation__pc,
                                              GW_Volunteers__Volunteer_Availability__pc,
                                              GW_Volunteers__Volunteer_Skills__pc,
                                              Special_Skills__c,
                                              Experience_Level_With_Cats__c,
                                              Experience_Level_with_Dogs__c,         
                                              Emergency_Contact_Name__pc,
                                              Emergency_Contact_Home_Phone__pc,
                                              Emergency_Contact_Work_Phone__pc,
                                              Emergency_Contact_Email__pc,
                                              Terms_Conditions_Accepted__pc,
                                              Age_Checkbox__c
                                           FROM Account
                                           WHERE PersonEmail = :account.PersonEmail OR Contact_Email__c = :account.PersonEmail LIMIT 1] ;
                       a.RecordType = personAccountRecordType;
                       a.Salutation = salutation;
                       a.FirstName = fname;
                       a.LastName = lname;
                       a.Contact_Email__c = account.PersonEmail;
                       a.PersonEmail = account.PersonEmail;
                       a.Volunteer_Application_Submitted__c = true;
                       a.PersonMailingStreet = account.PersonMailingStreet;
                       a.PersonMailingState  = account.PersonMailingState;
                       a.PersonMailingCity = account.PersonMailingCity;
                       a.PersonMailingPostalCode = account.PersonMailingPostalCode;
                       a.PersonHomePhone = account.PersonHomePhone;
                       a.Phone = account.Phone;
                       a.Allergies_and_or_Restrictions__pc = account.Allergies_and_or_Restrictions__pc;
                       a.How_did_you_hear_about_us__pc = account.How_did_you_hear_about_us__pc;
                       a.Current_Occupation__pc = account.Current_Occupation__pc;
                       a.GW_Volunteers__Volunteer_Availability__pc = account.GW_Volunteers__Volunteer_Availability__pc;
                       a.GW_Volunteers__Volunteer_Skills__pc = account.GW_Volunteers__Volunteer_Skills__pc;
                       a.Special_Skills__c = account.Special_Skills__c;
                       a.Experience_Level_With_Cats__c = account.Experience_Level_With_Cats__c;
                       a.Experience_Level_with_Dogs__c = account.Experience_Level_with_Dogs__c;
                       a.Emergency_Contact_Name__pc = account.Emergency_Contact_Name__pc;
                       a.Emergency_Contact_Home_Phone__pc = account.Emergency_Contact_Home_Phone__pc;
                       a.Emergency_Contact_Work_Phone__pc = account.Emergency_Contact_Work_Phone__pc;
                       a.Emergency_Contact_Email__pc = account.Emergency_Contact_Email__pc;
                       a.Terms_Conditions_Accepted__pc = account.Terms_Conditions_Accepted__pc;
                       a.Age_Checkbox__c = account.Age_Checkbox__c;
                       upsert a;
                      }
                    else
                       insert account;
                }        
            catch(Exception e){
                ApexPages.addMessages(e);
                return null;
            
                }
          PageReference pageRef = new PageReference('http://www.pawsatlanta.org/thank-you-for-saving-a-life');
          pageRef.setRedirect(true);
          return pageRef; 
  
          }
         Else {
             account.Terms_Conditions_Accepted__pc.addError('Please Accept Terms and Conditions');
             return null;
         }
     }
     Else {
     
            ApexPages.Message message = new ApexPages.message(ApexPages.severity.ERROR,'Person Account Already Exists');
            ApexPages.addMessage(message);
            return null;
     
     }
     
    
   }

 
    public String pawsLogo { get {
        document doc = [ SELECT Id, Name FROM Document WHERE Name = 'PAWS Logo' limit 1];
        string imageid = doc.id; 
        imageid = imageid.substring(0,15);
        return '/servlet/servlet.FileDownload?file=' + imageid;
        }  set; }
    
    
}

 
Krishna.AvvaKrishna.Avva
Hi Julianne Tajuba ,

Please make sure you select the last option to run the test cases while deploying the code. PFA. This will run only the test cases you need and not everything. User-added image

Please read the description against each of the options to understand the differences.
Let me know if this resolved your issue.

Regards,
Krishna Avva
Julianne TajubaJulianne Tajuba
Thanks for the reply. To be clear should I only run tests on my specific change set components? The things listed below are the only things that have been changed/added.Change Set Components
Rishab TyagiRishab Tyagi
Hello Julianne,

As I can see that you have only one Apex class in your change set. You'll need to add your test class in this list of components. This will be the test class that you have developed specifically to cover your code. If you have not written one already then please follow this trail to understand how to write one:
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

Once you have written the test class and successfully executed in your sandbox environment you'll be able to deploy the change set in your production environment. Do remember to add the test class in the change set. Use the Run Specific Tests and mention the name of your test class in the box.

Also, you need to make sure that your test class covers about 75% of your code in the main APEX class.

As for the null pointer exception, it occurs when you try to execute a function or access the property, from an object when the object itself is null
Eg:
String str;
System.debug(str.substring(1));
The above code will give exception as the value of str is null by default.
String str = 'test string';
System.debug(str.substring(1));
The above code will resolve the issue.

Hope that answers your question. 
Julianne TajubaJulianne Tajuba
Still getting the null exception with my test class.
I've also got an error at line 21 that says....(missing ';' at c) that I don't understand

@isTest

public class CTLR_VolunteerApplicationTest {
    public static testMethod void testCTLR_VolunteerApplication() {
    
    PageReference pageRef = new PageReference('/Volunteer_Application');
    Test.setCurrentPage(pageRef);
    
    Account c = new Account(RecordTypeId ='012i0000001DfXD', FirstName = 'Test', LastName = 'Test1', PersonEmail='test@test.com', Terms_Conditions_Accepted__pc = true);
    ApexPages.StandardController sc = new ApexPages.StandardController(c);
    CTLR_VolunteerApplication_Updates va = new CTLR_VolunteerApplication_Updates(sc);
    va.lname = 'Test1';
    va.fname = 'Test';
    PageReference nextPage = va.submit();
    // Verify that same Volunteer Application Thankyou page
    
    System.debug('value:'+nextPage.getUrl().toLowerCase());
    System.assertEquals(Page.Volunteer_Application_Thankyou.getUrl(), nextPage.getUrl().toLowerCase())    
        
  
    c = new Account(FirstName = 'Test', LastName = 'Test1', PersonEmail='test@test.com', Terms_Conditions_Accepted__pc = true);
    sc = new ApexPages.StandardController(c);
    va = new CTLR_VolunteerApplication_Updates(sc);
    va.lname = 'Test1';
    va.fname = 'Test';
    nextPage = va.submit();
    // Verify that same Volunteer Application Thankyou page
    System.assertEquals(null, nextPage);
   
    Document doc = new Document(Name = 'PAWS Logo', FolderId = '00li0000001DrR8');
    insert doc;
    String id = doc.Id;
    //verify image id returns
    System.assertEquals('/servlet/servlet.FileDownload?file=' + id.substring(0, 15), va.pawsLogo);
        
  
    
    c = null;
    sc= null;
    va = null;
    nextPage = null;
    c = new Account(FirstName = 'Test1', LastName = 'Test2', PersonEmail='test2@test.com');
    sc = new ApexPages.StandardController(c);
    va = new CTLR_VolunteerApplication_Updates(sc);
        va.lname = 'Test2';
    va.fname = 'Test1';
    nextPage = va.submit();
    // Verify that same Volunteer throws error message
    System.assertEquals(null, nextPage);
    
    List<ApexPages.Message> msgList = ApexPages.getMessages();
    // or loop over the messages
    for(ApexPages.Message msg :  ApexPages.getMessages()) {
  //      System.assertEquals('Please Accept Terms and Conditions', msg.getSummary());
  //System.assertEquals('Person Account Already Exists', msg.getSummary());
        System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity()); 
    }
    
    c = null;
    sc= null;
    va = null;
    nextPage = null;
    c = new Account(Terms_Conditions_Accepted__pc = true);
    sc = new ApexPages.StandardController(c);
    va = new CTLR_VolunteerApplication_Updates(sc);
    va.lname = null;
    va.fname = null;
    nextPage = va.submit();
    // Verify that same Volunteer throws error message
    System.assertEquals(null, nextPage);
    msgList = null;
    msgList = ApexPages.getMessages();
    boolean b = false;
    boolean a = false;
    // or loop over the messages
    for(ApexPages.Message msg :  ApexPages.getMessages()) {
        System.debug(msg.getDetail());
        if (msg.getDetail().contains('Please Accept Terms and Conditions')) b = true;
        if (msg.getDetail().contains('Required fields are missing: [LastName]')) a = true;
        
     
    }
    system.assert(b);
    system.assert(a);
    
    c = null;
    sc= null;
    va = null;
    nextPage = null;
    Account c1 = new Account(RecordTypeId ='012i0000001DfXD', FirstName = 'Test4', LastName = 'Test5', PersonEmail='test6@test.com', Terms_Conditions_Accepted__pc = true);
    insert c1;
    c = new Account(FirstName = 'Test4', LastName = 'Test5', PersonEmail='test6@test.com');
    sc = new ApexPages.StandardController(c);
    va = new CTLR_VolunteerApplication_Updates(sc);
    va.lname = 'Test4';
    va.fname = 'Test5';

    nextPage = va.submit();
    // Verify that same Volunteer throws error message
    System.assertEquals(null, nextPage);
    
    }