Fix some potential null pointer dereferences

I don't think there were any instances of these causing problems
but it's better to be safe than sorry.