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
Ajay Patel 54Ajay Patel 54 

null point exception

error:  when i was trying to insert the record  new  with empty Trigger_Change_record_type__c filed 

"""recordtypechange: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.recordtypechange: line 8, column 1""

trigger recordtypechange on Account (before insert) {
    IF(Trigger.Isbefore && Trigger.IsInsert ){
    Map< String,Id> typeMap = New Map< String,Id>();
    for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Account']) {
         typeMap.put(rt.DeveloperName,rt.Id);
    }       
    for (Account cc : trigger.new)  {
        if (cc.Trigger_Change_record_type__c.contains('Labs') && cc.Trigger_Change_record_type__c !=null) { 
            if(typeMap.containsKey('bussiness'))
                cc.RecordTypeid = typeMap.get('bussiness');  
        }
    }
    }
}
 Thanks 
AnudeepAnudeep (Salesforce Developers) 
Hi Ajay, 

I recommend checking using the following in your code instead
String.isBlank(cc.Trigger_Change_record_type__c) 

The documentation says:

Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.


Null pointer exceptions are usually thrown by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.

I see you have the following query in the code. Also, it is a best practice to check for the presence of values before accessing the values. You can use the isEmpty() attribute for both list and map or use the safenavigator operator to avoid null pointer exceptions
 
Select DeveloperName, Id From RecordType Where sObjectType = 'Account


https://help.salesforce.com/articleView?id=000327918&type=1&mode=1
Ajay Patel 54Ajay Patel 54

Thanks @Anudeep  
but errror is same