Check for NULL before accessing structs

This should avoid segmentation faults and potential bugs should be easier to find.