#20024: Added 'if's to wherever a loop iterates over a variable which could potentially be null / undefined