You need to sign in to do that
Don't have an account?

Help with Test class error message
I am trying to write test coverage for a method and receiving the error "Attempt to de-reference a null object" when I run the test. Below is my test class, can anyone guide me in the right direction with this?
Test class:
static testmethod void getselectOptionsTest(){
Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Franchisee').getRecordTypeId();
Account fAcct = new Account(Name = 'Test', RecordTypeId=RecordTypeIdPropAccount);
insert fAcct;
sObject objObject = [SELECT Id, Name FROM Account WHERE Name = 'Test' LIMIT 1];
String fld = 'RetailerCategory';
Test.startTest();
multiPicklistCtrl.getselectOptions(objObject, fld);
Test.stopTest();
}
Original method:
@AuraEnabled
public static List <String> getselectOptions(sObject objObject, string fld) {
system.debug('objObject --->' + objObject);
system.debug('fld --->' + fld);
List < String > allCats = new list < String > ();
// Get the object type of the SObject.
Schema.sObjectType objType = objObject.getSObjectType();
// Describe the SObject using its object type.
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
// Get a map of fields for the SObject
map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();
// Get the list of picklist values for this field.
list < Schema.PicklistEntry > values =
fieldMap.get(fld).getDescribe().getPickListValues();
// Add these values to the selectoption list.
for (Schema.PicklistEntry a: values) {
allCats.add(a.getValue());
}
system.debug('allCats ---->' + allCats);
allCats.sort();
return allCats;
}
Test class:
static testmethod void getselectOptionsTest(){
Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Franchisee').getRecordTypeId();
Account fAcct = new Account(Name = 'Test', RecordTypeId=RecordTypeIdPropAccount);
insert fAcct;
sObject objObject = [SELECT Id, Name FROM Account WHERE Name = 'Test' LIMIT 1];
String fld = 'RetailerCategory';
Test.startTest();
multiPicklistCtrl.getselectOptions(objObject, fld);
Test.stopTest();
}
Original method:
@AuraEnabled
public static List <String> getselectOptions(sObject objObject, string fld) {
system.debug('objObject --->' + objObject);
system.debug('fld --->' + fld);
List < String > allCats = new list < String > ();
// Get the object type of the SObject.
Schema.sObjectType objType = objObject.getSObjectType();
// Describe the SObject using its object type.
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
// Get a map of fields for the SObject
map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();
// Get the list of picklist values for this field.
list < Schema.PicklistEntry > values =
fieldMap.get(fld).getDescribe().getPickListValues();
// Add these values to the selectoption list.
for (Schema.PicklistEntry a: values) {
allCats.add(a.getValue());
}
system.debug('allCats ---->' + allCats);
allCats.sort();
return allCats;
}
All Answers
I think the issue is coming as there is no recordtype on account named Franchisee.
Could you check at your end that there is no typo error in account recordtype.
Hope this helps!
If it's a custom field, change it to RetailerCategory__c and see if that works.