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
ShadowlessKickShadowlessKick 

Null Errors

Could someone explain how you convert null value errors returning from fields collected in an SOQL Query? 

 

Error: System.NullPointerException: Argument 1 cannot be null

 

Opportunity theOpportunity = new Opportunity();  

for (Opportunity theOpportunity:[select Id, Quote__c from Opportunity Limit 1]) 
{
		String TestField;
   		TestField = String.valueOf(theOpportunity.get('Quote__c'));
		System.debug('The Value 1 :' + TestField);     
	 	TestField = String.valueOf(theOpportunity.Quote__c);
	 	System.debug('The Value 2 :' + TestField);     
}

 Would have thought that the String.valueOf would convert null to ''.

Best Answer chosen by Admin (Salesforce Developers) 
ShadowlessKickShadowlessKick

Thank you for your help.  I was hoping there was a magic APEX function that would convert a NULL value to an empty string.  It looks like that has to be coded manually.
 

All the fields that could possibly end up with a value of NULL were collected in a list.  Through a loop, each field was checked replacing NULL with an empty string. This method does not handle NULL dates.
 

None of the fields could be NULL because they were being used in an XML "writeCharacters" function.  The NULL values were throwing an error.

 

 

public List<string> nullfieldlist { get; set; }
  
nullfieldlist = new List<String>();
nullfieldlist.add('Sales_Representative__c');
nullfieldlist.add('Field_Sales_Rep_Number__c');
nullfieldlist.add('Market_Code__c');
nullfieldlist.add('Quote__c');
           
for (Opportunity theOpportunity:[select Id, Name, LastModifiedDate, LastModifiedBy.name, Sales_Representative__c, Field_Sales_Rep_Number__c, Market_Code__c, Quote__c, CreatedDate From Opportunity Limit 1]) 
{
		
for (string thefieldname: nullfieldlist) {
	try {

	 if (theOpportunity.get(thefieldname) == null){
		theOpportunity.put(thefieldname, '');
	    }

	} catch(Exception e) 

	{             
	System.debug('Field Null Exception ' + e);  
	} 

}
    		
} //End Loop for Opportunity 

 

Xmlstreamwriter w = new Xmlstreamwriter();

w.writeCharacters(String.valueOf(theOpportunity.get('Quote__c')));

 

 

All Answers

Adam HowarthAdam Howarth

Salesforce has a lot of quirks like this that are very annoying.

 

Try this though:

 

Opportunity theOpportunity = new Opportunity();  

for (Opportunity theOpportunity:[select Id, Quote__c from Opportunity Limit 1]) 
{
		String TestField;
   		TestField = String.valueOf(theOpportunity.get('Quote__c') == null ? 'null' : theOpportunity.get('Quote__c'));
		System.debug('The Value 1 :' + TestField);     
	 	TestField = String.valueOf(theOpportunity.Quote__c == null ? 'null' : theOpportunity.Quote__c);
	 	System.debug('The Value 2 :' + TestField);     
}
imutsavimutsav
Try this 


Opportunity theOpportunity = new Opportunity(); List<Opportunity> theOpportunity = [select Id, Quote__c from Opportunity Limit 1];
if(theOpportunity!=NULL && theOpportunity.get(0)!=NULL) {

String TestField;
try { TestField = String.valueOf(theOpportunity.get('Quote__c'));
} catch(Exception e) {
System.debug('----------Exception ' + e);
TestField = NULL;
} System.debug('The Value 1 :' + TestField);
try { TestField = String.valueOf(theOpportunity.Quote__c);
} catch(Exception e) {
System.debug('-----------Exception ' + e);
TestField = NULL;
}
System.debug('The Value 2 :' + TestField); }


Thanks
Utsav

[Do mark this answer as solution if it works for you and give a kudos.]

ShadowlessKickShadowlessKick

Thank you for your help.  I was hoping there was a magic APEX function that would convert a NULL value to an empty string.  It looks like that has to be coded manually.
 

All the fields that could possibly end up with a value of NULL were collected in a list.  Through a loop, each field was checked replacing NULL with an empty string. This method does not handle NULL dates.
 

None of the fields could be NULL because they were being used in an XML "writeCharacters" function.  The NULL values were throwing an error.

 

 

public List<string> nullfieldlist { get; set; }
  
nullfieldlist = new List<String>();
nullfieldlist.add('Sales_Representative__c');
nullfieldlist.add('Field_Sales_Rep_Number__c');
nullfieldlist.add('Market_Code__c');
nullfieldlist.add('Quote__c');
           
for (Opportunity theOpportunity:[select Id, Name, LastModifiedDate, LastModifiedBy.name, Sales_Representative__c, Field_Sales_Rep_Number__c, Market_Code__c, Quote__c, CreatedDate From Opportunity Limit 1]) 
{
		
for (string thefieldname: nullfieldlist) {
	try {

	 if (theOpportunity.get(thefieldname) == null){
		theOpportunity.put(thefieldname, '');
	    }

	} catch(Exception e) 

	{             
	System.debug('Field Null Exception ' + e);  
	} 

}
    		
} //End Loop for Opportunity 

 

Xmlstreamwriter w = new Xmlstreamwriter();

w.writeCharacters(String.valueOf(theOpportunity.get('Quote__c')));

 

 

This was selected as the best answer