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
Afrose AhamedAfrose Ahamed 

caused by: System.NullPointerException: Argument cannot be null.

Hi All,

At line 13 im getting error System.NullPointerException: Argument cannot be null. Please help me guys how can i sort out this issue
Error Line Highlighted in Bold
 

public with sharing class checkRec{
    public List<sObject> soblist {get;set;}
    public String oblabel { get;set;}
    public String Obtype {get;set;} //Object API Name
    public List<Schema.FieldSetMember> fls {get;set;} //The fields from the fieldset
    public String obql {get;set;}
    public Search__c Search {get;set;}
    public String spquery { get;set; }
    public String spqueryid { get;set; }
    
    public checkRec(){
        //set Object Type and Fields(Error Line)
        evt= String.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('Obtype '));
        if(Obtype == getNamePrefix.getNamePrefix()+'Check__c')
            Obtype = getNamePrefix.getNamePrefix()+'Note__c';
        if(Obtype == getNamePrefix.getNamePrefix()+'Source__c')
            Obtype = getNamePrefix.getNamePrefix()+'Data_Source__c';    
        if(Obtype == getNamePrefix.getNamePrefix()+'User_Details__C')
            Obtype = 'User';    
            
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();             
        oblabel= globalDescription.get(Obtype ).getDescribe().getLabel();
        Schema.SObjectType SObjectTypeObj = globalDescription.get(Obtype);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
        try{
            Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(getNamePrefix.getNamePrefix()+'LookUp');  
            fields = fieldSetObj.getFields();

        } catch(exception e){
             System.debug('Add Loos Field Set to this object');
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'This Object does not have a \'checkRec\' Field Set'));
        }        
        Search__c = new Search__c();
   
        runlooks();
              
    }
 

   Thanks,
Afrose Ahamed M.G.

Best Answer chosen by Afrose Ahamed
mukesh guptamukesh gupta
Hi Afrose,

Reason behind:- You are not Passing parameter "Obtype" in URL because of that it's give you error.
To handle this write if condition to check null value of that parameter.
see below updated class code.

 
public with sharing class checkRec{
    public List<sObject> soblist {get;set;}
    public String oblabel { get;set;}
    public String Obtype {get;set;} //Object API Name
    public List<Schema.FieldSetMember> fls {get;set;} //The fields from the fieldset
    public String obql {get;set;}
    public Search__c Search {get;set;}
    public String spquery { get;set; }
    public String spqueryid { get;set; }
	public String evt {get;set;}
    
    public checkRec(){
        //set Object Type and Fields(Error Line)
		if(ApexPages.currentPage().getParameters().get('Obtype') != null){
			evt= String.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('Obtype'));
		}
        if(Obtype == getNamePrefix.getNamePrefix()+'Check__c')
            Obtype = getNamePrefix.getNamePrefix()+'Note__c';
        if(Obtype == getNamePrefix.getNamePrefix()+'Source__c')
            Obtype = getNamePrefix.getNamePrefix()+'Data_Source__c';    
        if(Obtype == getNamePrefix.getNamePrefix()+'User_Details__C')
            Obtype = 'User';    
            
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();             
        oblabel= globalDescription.get(Obtype ).getDescribe().getLabel();
        Schema.SObjectType SObjectTypeObj = globalDescription.get(Obtype);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
        try{
            Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(getNamePrefix.getNamePrefix()+'LookUp');  
            fields = fieldSetObj.getFields();

        } catch(exception e){
             System.debug('Add Loos Field Set to this object');
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'This Object does not have a \'checkRec\' Field Set'));
        }        
        Search__c = new Search__c();
   
        runlooks();
              
    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Afrose,

Reason behind:- You are not Passing parameter "Obtype" in URL because of that it's give you error.
To handle this write if condition to check null value of that parameter.
see below updated class code.

 
public with sharing class checkRec{
    public List<sObject> soblist {get;set;}
    public String oblabel { get;set;}
    public String Obtype {get;set;} //Object API Name
    public List<Schema.FieldSetMember> fls {get;set;} //The fields from the fieldset
    public String obql {get;set;}
    public Search__c Search {get;set;}
    public String spquery { get;set; }
    public String spqueryid { get;set; }
	public String evt {get;set;}
    
    public checkRec(){
        //set Object Type and Fields(Error Line)
		if(ApexPages.currentPage().getParameters().get('Obtype') != null){
			evt= String.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('Obtype'));
		}
        if(Obtype == getNamePrefix.getNamePrefix()+'Check__c')
            Obtype = getNamePrefix.getNamePrefix()+'Note__c';
        if(Obtype == getNamePrefix.getNamePrefix()+'Source__c')
            Obtype = getNamePrefix.getNamePrefix()+'Data_Source__c';    
        if(Obtype == getNamePrefix.getNamePrefix()+'User_Details__C')
            Obtype = 'User';    
            
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();             
        oblabel= globalDescription.get(Obtype ).getDescribe().getLabel();
        Schema.SObjectType SObjectTypeObj = globalDescription.get(Obtype);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
        try{
            Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(getNamePrefix.getNamePrefix()+'LookUp');  
            fields = fieldSetObj.getFields();

        } catch(exception e){
             System.debug('Add Loos Field Set to this object');
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'This Object does not have a \'checkRec\' Field Set'));
        }        
        Search__c = new Search__c();
   
        runlooks();
              
    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
Afrose AhamedAfrose Ahamed

Hi @mukesh gupta,

Thanks for you reply,

Yes, I added the line. However when im clicking the preview its im getting the same error.  Is possible to add return null in constructor. I Hope we cannot add return null in constructor. Please advise.

Thanks in advance.

 

Afrose

mukesh guptamukesh gupta
Yes we can't return to construcor.

Don't test by preview button, just go to actual record and check 

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
mukesh guptamukesh gupta
Hi Ahamed,

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh