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
Manju053Manju053 

How to Resolve the error System.NullPointerException: Attempt to de-reference a null object

i have  designed a apex class on custom object where it checks the duplicate value for Specific recordtype, it is invoked by a trigger handler, i am getting this error - 

QuotaTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.QuotaTriggerHandler.dupQuotaError: line 5, column 1 Trigger.QuotaTrigger: line 3, column 1

I tried Googling the error but still   not able to Resolve the error, Any suggestions please

QuotaTrigger
 
trigger QuotaTrigger on Quota__c (before insert, before update) {

    QuotaTriggerHandler.dupQuotaError(Trigger.new);
    
}

QuotaTriggerHandler
 
public class QuotaTriggerHandler {
    
    public static void dupQuotaError (List <Quota__c> newTrigCon) {
        
        Id recordTypeId = Schema.SObjectType.Quota__c.getRecordTypeInfosByDeveloperName().get('Inside Sales Target(IS)').getRecordTypeId();
        
        //List of all Contact Records currently in the database    
        List <Quota__c> allCon = [SELECT ID, Name, Assignedto__c, Quater_Year__c, RecordTypeId, Month__c FROM Quota__c where RecordTypeId=:recordTypeId];
        
        //Iterate through new potential records
        for (Quota__c oneConTrig : newTrigCon) {
            
            //Needed to iterate through all existing records which is contained in the allCon List
            for (Integer i = 0; i < allCon.size(); i++)
                
                //If all conditions are met, there is a duplicate and thus, returns an error.
                if (oneContrig.Assignedto__c == allCon[i].Assignedto__c 
                    && oneContrig.Quater_Year__c == allCon[i].Quater_Year__c 
                    && oneConTrig.Month__c == allCon[i].Month__c
                    && oneConTrig.RecordTypeId == recordTypeId) {
                        
                        oneConTrig.addError('Duplicate Quota Detected! This record already exists.');
                        
                    }
        }      
    }
}

How do i resove this issue
Best Answer chosen by Manju053
Suraj MakandarSuraj Makandar
Hi Rumy,

We get null pointer exception when we try to access any method/value of null object/record.

You are getting this error at line no. 5 because your below method is returnig null value:
 
Schema.SObjectType.Quota__c.getRecordTypeInfosByDeveloperName().get('Inside Sales Target(IS)')

You have to provide developer name of your Record Type to above method OR you can use method given below, which retrives Record Type ID by its name.
Id recordTypeId = Schema.SObjectType.Quota__c.getRecordTypeInfosByName().get('Inside Sales Target(IS)').getRecordTypeId();

Thanks,
Suraj
 

All Answers

Suraj MakandarSuraj Makandar
Hi Rumy,

We get null pointer exception when we try to access any method/value of null object/record.

You are getting this error at line no. 5 because your below method is returnig null value:
 
Schema.SObjectType.Quota__c.getRecordTypeInfosByDeveloperName().get('Inside Sales Target(IS)')

You have to provide developer name of your Record Type to above method OR you can use method given below, which retrives Record Type ID by its name.
Id recordTypeId = Schema.SObjectType.Quota__c.getRecordTypeInfosByName().get('Inside Sales Target(IS)').getRecordTypeId();

Thanks,
Suraj
 
This was selected as the best answer
Manju053Manju053
@Suraj

Woow thanks suraj, it worked perfectly,:)