+ Start a Discussion
TexasWheelingTexasWheeling 

"Attempt to de-reference a null object" error when deploying an apex trigger to Production

I'm having some difficulty with deploying an Apex Trigger and its Test Class. The trigger works great in my Sandbox environment, and the test class covers 96% code coverage. I'm fairly new to Apex coding and can not seem to figure out what is causing the error. Any suggestions would be great!


Apex Trigger Code:

trigger activeWebsites on Account (before Insert, before Update) {
    //Updating the "Active Website(s)" field
    for(Account a : Trigger.New) {
        // Ford (Elite)
        if(a.FordElite_Status__c == 'Live' && a.Lincoln_Elite_Status__c != 'Live' && a.Lincoln_Base_Status__c != 'Live') {
            a.Active_Website_s__c = 'Ford (Elite)';
        }
        //Ford (Elite) & Lincoln (Base)
        else if(a.FordElite_Status__c == 'Live' && a.Lincoln_Elite_Status__c != 'Live' && a.Lincoln_Base_Status__c == 'Live') {
            a.Active_Website_s__c = 'Ford (Elite) & Lincoln (Base)';
        }
        //Ford (Elite) & Lincoln (Elite)
        else if(a.FordElite_Status__c == 'Live' && a.Lincoln_Elite_Status__c == 'Live' && a.Lincoln_Base_Status__c != 'Live') {
            a.Active_Website_s__c = 'Ford (Elite) & Lincoln (Elite)';
        }
        //Lincoln (Base)
        else if(a.FordElite_Status__c != 'Live' && a.Lincoln_Elite_Status__c != 'Live' && a.Lincoln_Base_Status__c == 'Live') {
            a.Active_Website_s__c = 'Lincoln (Base)';
        }
        //Lincoln (Elite)
        else if(a.FordElite_Status__c != 'Live' && a.Lincoln_Elite_Status__c == 'Live' && a.Lincoln_Base_Status__c != 'Live') {
            a.Active_Website_s__c = 'Lincoln (Elite)';
        }
        //Toyota (Lite)
        else if(((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.FUSION_WEB__c == 'Core' && a.Web_Effective_Date__c != null && a.Scion_Web__c != 'Pro' && a.Scion_Web_Effective_Date__c == null) || ((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.FUSION_WEB__c == 'Core' && a.Web_Effective_Date__c != null && a.Scion_Web__c == 'Pro' && a.Scion_Web_Effective_Date__c == null) || ((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.FUSION_WEB__c == 'Core' && a.Web_Effective_Date__c != null && a.Scion_Web__c != 'Pro' && a.Scion_Web_Effective_Date__c != null)) {
            a.Active_Website_s__c = 'Toyota (Lite)';
        }
        //Toyota (Pro)
        else if(((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.FUSION_WEB__c == 'Pro' && a.Web_Effective_Date__c != null && a.Scion_Web__c != 'Pro' && a.Scion_Web_Effective_Date__c == null) || ((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.FUSION_WEB__c == 'Pro' && a.Web_Effective_Date__c != null && a.Scion_Web__c == 'Pro' && a.Scion_Web_Effective_Date__c == null) || ((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.FUSION_WEB__c == 'Pro' && a.Web_Effective_Date__c != null && a.Scion_Web__c != 'Pro' && a.Scion_Web_Effective_Date__c != null)) {
            a.Active_Website_s__c = 'Toyota (Pro)';
        }
        //Toyota (Lite) & Scion
        else if((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.Franchise_s__c.contains('Scion') && a.FUSION_WEB__c == 'Core' && a.Web_Effective_Date__c != null && a.Scion_Web__c == 'Pro' && a.Scion_Web_Effective_Date__c != null) {
            a.Active_Website_s__c = 'Toyota (Lite) & Scion';
        }
        //Toyota (Pro) & Scion
        else if((a.RecordTypeId == '012A0000000q4l3' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c.contains('Toyota') && a.Franchise_s__c.contains('Scion') && a.FUSION_WEB__c == 'Pro' && a.Web_Effective_Date__c != null && a.Scion_Web__c == 'Pro' && a.Scion_Web_Effective_Date__c != null) {
            a.Active_Website_s__c = 'Toyota (Pro) & Scion';
        }
        //Scion
        else if((a.Franchise_s__c.contains('Scion') && a.Scion_Web__c == 'Pro' && a.Scion_Web_Effective_Date__c != null && (a.FUSION_WEB__c != 'Core' || a.FUSION_WEB__c != 'Pro') && a.Web_Effective_Date__c == null) || (a.Franchise_s__c.contains('Scion') && a.Scion_Web__c == 'Pro' && a.Scion_Web_Effective_Date__c != null && (a.FUSION_WEB__c == 'Core' || a.FUSION_WEB__c == 'Pro') && a.Web_Effective_Date__c == null) || (a.Franchise_s__c.contains('Scion') && a.Scion_Web__c == 'Pro' && a.Scion_Web_Effective_Date__c != null && (a.FUSION_WEB__c != 'Core' || a.FUSION_WEB__c != 'Pro') && a.Web_Effective_Date__c != null)) {
            a.Active_Website_s__c = 'Scion';
        }
        //Acura (Core)
        else if(a.Franchise_s__c == 'Acura' && a.FUSION_WEB__c == 'Core' && a.Web_Effective_Date__c != null) {
            a.Active_Website_s__c = 'Acura (Core)';
        }
        //Acura (Pro)
        else if(a.Franchise_s__c == 'Acura' && a.FUSION_WEB__c == 'Premium' && a.Web_Effective_Date__c != null) {
            a.Active_Website_s__c = 'Acura (Pro)';
        }
        //Honda
        else if(a.Franchise_s__c == 'Honda' && a.FUSION_WEB__c == 'Pro' && a.Web_Effective_Date__c != null) {
            a.Active_Website_s__c = 'Honda';
        }
        //Other
        else if((a.RecordTypeId == '012A0000000q4l1' || a.RecordTypeId == '012G0000000qF9V' || a.RecordTypeId == '012G0000000qEhR') && a.Franchise_s__c != 'Acura' && a.Franchise_s__c != 'Honda' && a.FUSION_WEB__c == 'Pro' && a.Web_Effective_Date__c != null) {
            a.Active_Website_s__c = 'Other';
        }
        //Null
        else {
            a.Active_Website_s__c = null;
        }
    }
}


Apex Text Class:

@isTest
public class Test_activeWebsites {
    static testMethod void testRecordCreation() {
  
    //Create Ford (Elite) Account
    Account a1 = new Account();
    a1.RecordTypeId = '012G0000000qG8j';
    a1.Name = 'Ford (Elite)';
    a1.Franchise_s__c = 'Ford';
    a1.FordElite_Status__c = 'Live';
    insert a1;
  
    //Create Ford (Elite) & Lincoln (Base) Account
    Account a2 = new Account();
    a2.RecordTypeId = '012G0000000qH4j';
    a2.Name = 'Ford (Elite) & Lincoln (Base)';
    a2.Franchise_s__c = 'Ford; Lincoln';
    a2.FordElite_Status__c = 'Live';
    a2.Lincoln_Base_Status__c = 'Live';
    insert a2;
  
    //Create Ford (Elite) & Lincoln (Elite) Account
    Account a3 = new Account();
    a3.RecordTypeId = '012G0000000qH4j';
    a3.Name = 'Ford (Elite) & Lincoln (Elite)';
    a3.Franchise_s__c = 'Ford; Lincoln';
    a3.FordElite_Status__c = 'Live';
    a3.Lincoln_Elite_Status__c = 'Live';
    insert a3;
  
    //Create Lincoln (Base) Account
    Account a4 = new Account();
    a4.RecordTypeId = '012G0000000qH4e';
    a4.Name = 'Lincoln (Base)';
    a4.Franchise_s__c = 'Lincoln';
    a4.Lincoln_Base_Status__c = 'Live';
    insert a4;
  
    //Create Lincoln (Elite) Account
    Account a5 = new Account();
    a5.RecordTypeId = '012G0000000qH4e';
    a5.Name = 'Lincoln (Elite)';
    a5.Franchise_s__c = 'Lincoln';
    a5.Lincoln_Elite_Status__c = 'Live';
    insert a5;
  
    //Create Toyota (Lite) Account
    Account a6 = new Account();
    a6.RecordTypeId = '012A0000000q4l3';
    a6.Name = 'Toyota (Lite)';
    a6.Franchise_s__c = 'Toyota';
    a6.FUSION_WEB__c = 'Core';
    a6.Web_Effective_Date__c = system.TODAY();
    insert a6;
  
    //Create Toyota (Pro) Account
    Account a7 = new Account();
    a7.RecordTypeId = '012A0000000q4l3';
    a7.Name = 'Toyota (Pro)';
    a7.Franchise_s__c = 'Toyota';
    a7.FUSION_WEB__c = 'Pro';
    a7.Web_Effective_Date__c = system.TODAY();
    insert a7;
  
    //Create Toyota (Lite) & Scion Account
    Account a8 = new Account();
    a8.RecordTypeId = '012A0000000q4l3';
    a8.Name = 'Toyota (Lite) & Scion';
    a8.Franchise_s__c = 'Toyota; Scion';
    a8.FUSION_WEB__c = 'Core';
    a8.Web_Effective_Date__c = system.TODAY();
    a8.Scion_Web__c = 'Pro';
    a8.Scion_Web_Effective_Date__c = system.TODAY();
    insert a8;
  
    //Create Toyota (Pro) & Scion Account
    Account a9 = new Account();
    a9.RecordTypeId = '012A0000000q4l3';
    a9.Name = 'Toyota (Pro) & Scion';
    a9.Franchise_s__c = 'Toyota; Scion';
    a9.FUSION_WEB__c = 'Pro';
    a9.Web_Effective_Date__c = system.TODAY();
    a9.Scion_Web__c = 'Pro';
    a9.Scion_Web_Effective_Date__c = system.TODAY();
    insert a9;
  
    //Create Scion Account
    Account a10 = new Account();
    a10.RecordTypeId = '012A0000000q4l3';
    a10.Name = 'Scion';
    a10.Franchise_s__c = 'Scion';
    a10.Scion_Web__c = 'Pro';
    a10.Scion_Web_Effective_Date__c = system.TODAY();
    insert a10;
  
    //Create Acura (Core) Account
    Account a11 = new Account();
    a11.RecordTypeId = '012G0000000qF9V';
    a11.Name = 'Acura (Core)';
    a11.Franchise_s__c = 'Acura';
    a11.FUSION_WEB__c = 'Core';
    a11.Web_Effective_Date__c = system.TODAY();
    insert a11;
  
    //Create Acura (Pro) Account
    Account a12 = new Account();
    a12.RecordTypeId = '012G0000000qF9V';
    a12.Name = 'Acura (Pro)';
    a12.Franchise_s__c = 'Acura';
    a12.FUSION_WEB__c = 'Premium';
    a12.Web_Effective_Date__c = system.TODAY();
    insert a12;
  
    //Create Honda Account
    Account a13 = new Account();
    a13.RecordTypeId = '012G0000000qF9V';
    a13.Name = 'Honda';
    a13.Franchise_s__c = 'Honda';
    a13.FUSION_WEB__c = 'Pro';
    a13.Web_Effective_Date__c = system.TODAY();
    insert a13;
  
    //Create Other Account
    Account a14 = new Account();
    a14.RecordTypeId = '012A0000000q4l1';
    a14.Name = 'Other';
    a14.Franchise_s__c = 'Jeep';
    a14.FUSION_WEB__c = 'Pro';
    a14.Web_Effective_Date__c = system.TODAY();
    insert a14;
  
    }
}


Deployment Failure Error:

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, activeWebsites: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.activeWebsites: line 41, column 1: []", Failure Stack Trace: "Class.M...
GunnarGunnar
You make reference to the ID's.
So the exact id must exist in sandbox and production.

Did you create a record type in production or sandbox AFTER you copied the sandbox? If so, there is your problem.

You may want to do a SELECT on RecordType to get them Then say the account is of record type WHERE the record type has this name/description.

This will keep the program dynamic and able to move without relying on hard-wired record type id's.

TexasWheelingTexasWheeling
I tried using the SELECT queuery listed below instead of using the Record Type ID number but continued to get the same error.

[SELECT Id FROM RecordType WHERE Name = 'record type name'].Id

I just replaced the "record type name" portion of the queuery with the correct name of the record type I intended to use. Am I doing something wrong with the queuery?
TexasWheelingTexasWheeling
I was able to get the Apex Trigger to deploy into my Production environment as the original error was coming from another Apex Class causing some issues.

I am now trying to perform a bulk upload on my Account object, and I am now receiving this error message.

Apex script unhandled trigger exception by user/organization: 005A0000000eCZI/00DA0000000IUKq
activeWebsites: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.activeWebsites: line 41, column 1

Any ideas as to what might be causing this? Again, I'm still fairly new to this and have had trouble troubleshooting this error.
TexasWheelingTexasWheeling
So I have found the source of the issue, but I am lost how to correct the issue due to my lack of Apex Code knowledge. It appears that when I leave the "Franchise_s__c" (a multi-select picklist field) empty on an Account, it causes the above error when editing the record. If there is a value entered into the "Franchise_s__c" field, I don't receive the error.

My guess is that my Apex Trigger code is requiring this field to have a value for some reason, but I can't seem to figure out where in my code this requirement is coming from. I'm betting that this is a simple coding syntax fix or something simple that I'm just overlooking.

Any help with this would be great appreciated!