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
David Brachten 6David Brachten 6 

Can't find the reason for nullpointer exeption

Hi all.

I am trying to build a class where I can pass a name of an object.
This class should search all fields of that object.
if one of the fields is of DataType Picklist, it should E-Mail me the Picklist values.

My problem is: I am gettin a de-refernence Null error, but I can't find the line where it is null?!
 
private void sendpicklistvalues(String theObject, String theField){
Schema.DescribeFieldResult F = Schema.getGlobalDescribe().get(theObject).getDescribe().fields.getMap().get(theField).getDescribe();    
List<Schema.PicklistEntry> P = F.getPicklistValues();
String generatedCSVFile = theField + '\n';
List<String> queryFields = new List<String>{'Label','value'};
String fileRow = '';
for(Schema.PicklistEntry e: P){
fileRow = '';
fileRow = fileRow + e.getLabel();
fileRow = fileRow +','+ e.getValue();
generatedCSVFile = generatedCSVFile + fileRow + '\n';
}
Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
Blob csvBlob = blob.valueOf(generatedCSVFile);
String csvName = 'picklistvalues.csv';
csvAttachment.setFileName(csvName);
csvAttachment.setBody(csvBlob);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'david.brachten@qiagen.com'};
String subject = 'picklist.csv';
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody(theField + ' - picklist labels and values');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}

private void describepicklists(String ofObject){
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(ofObject);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
for (String fieldName: fieldMap.keySet()) {
Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
if(fielddataType != Schema.DisplayType.Picklist) {
String thefieldname = fieldMap.get(fieldName).getDescribe().getLabel();
sendpicklistvalues(ofObject, thefieldname);
}
}
}

describepicklists('Account');
schemaMap and fieldMap are assignt so they are not null...
VARIABLE_ASSIGNMENT|[29]|schemaMap|"{account=Account, account_historic.....
VARIABLE_ASSIGNMENT|[31]|fieldMap|"{abc_classification__c=ABC_Cl

Does anybody see my mistake by chance?
 
Best Answer chosen by David Brachten 6
mrkimrki
You are currently passing the field label to the sendpicklistvalues() method instead of field name. The following statement returns null because the field you are trying to retrieve does not have the same name and label.
Schema.getGlobalDescribe().get(theObject).getDescribe().fields.getMap().get(theField)

Additonally please note that the following code statement results in non-picklist values to enter the sendpicklist values method:
if(fielddataType != Schema.DisplayType.Picklist)

Overall, I'd suggest that you modify the signature of the sendpicklistvalues to accept the Schema.SObjectField instead of the fieldname String. In the end you have already looked up for the Schema.SObjectField in the describepicklists and there is little to no sense in going through the chain from global describe back to the field.  You would also have avoided the NPE in the first place if you had passed the field onwards.The following code snippet can be adjusted easily to your use case.
 
private void sendpicklistvalues(String theObject, Schema.SObjectField theField){
	List<Schema.PicklistEntry> P = theField.getDescribe().getPicklistValues();
    //Insert remaining logic here
}

private void describepicklists(String ofObject){
	Schema.SObjectType leadSchema = Schema.getGlobalDescribe().get(ofObject);
	Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
	for (String fieldName: fieldMap.keySet()) {
		Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
		if(fielddataType == Schema.DisplayType.Picklist) {
			sendpicklistvalues(ofObject, fieldMap.get(fieldName));
		}
	}
}

All Answers

mrkimrki
You are currently passing the field label to the sendpicklistvalues() method instead of field name. The following statement returns null because the field you are trying to retrieve does not have the same name and label.
Schema.getGlobalDescribe().get(theObject).getDescribe().fields.getMap().get(theField)

Additonally please note that the following code statement results in non-picklist values to enter the sendpicklist values method:
if(fielddataType != Schema.DisplayType.Picklist)

Overall, I'd suggest that you modify the signature of the sendpicklistvalues to accept the Schema.SObjectField instead of the fieldname String. In the end you have already looked up for the Schema.SObjectField in the describepicklists and there is little to no sense in going through the chain from global describe back to the field.  You would also have avoided the NPE in the first place if you had passed the field onwards.The following code snippet can be adjusted easily to your use case.
 
private void sendpicklistvalues(String theObject, Schema.SObjectField theField){
	List<Schema.PicklistEntry> P = theField.getDescribe().getPicklistValues();
    //Insert remaining logic here
}

private void describepicklists(String ofObject){
	Schema.SObjectType leadSchema = Schema.getGlobalDescribe().get(ofObject);
	Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
	for (String fieldName: fieldMap.keySet()) {
		Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
		if(fielddataType == Schema.DisplayType.Picklist) {
			sendpicklistvalues(ofObject, fieldMap.get(fieldName));
		}
	}
}
This was selected as the best answer
David Brachten 6David Brachten 6
Thank you!!!!