+ Start a Discussion

How to handle null fields in query results

This is something I struggle with a lot, to the extent that I wonder if I'm approaching it wrong.


Suppose I have a custom object like Job_Application__c and I get an instance from the database:


Job_Application__c[] appList = [ select Id, Name, Date_Submitted__c from Job_Application__c];
Job_Application__c theApp = appList[0];
theApp.Date_Evaluate_By__c = theApp.Date_Submitted__c + 7;



In my experience, when theApp.Date_Submitted__c is null, this code gives a null pointer exception. To me this is flabbergasting. A null value in a field isn't an error or exceptional case. It happens all the time.


As a result, in our code, we have to test each and every field reference to determine if it's null before using it.


Is this considered normal behavior and practice in Apex programming, or have we missed something?