You need to sign in to do that
Don't have an account?
David 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?!
VARIABLE_ASSIGNMENT|[29]|schemaMap|"{account=Account, account_historic.....
VARIABLE_ASSIGNMENT|[31]|fieldMap|"{abc_classification__c=ABC_Cl
Does anybody see my mistake by chance?
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?
Additonally please note that the following code statement results in non-picklist values to enter the sendpicklist values method:
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.
All Answers
Additonally please note that the following code statement results in non-picklist values to enter the sendpicklist values method:
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.