Fix a few NullPointerExceptions identified by Eclipse

Eclipse's static analysis of nulls identifies a number of spots where a `NullPointerException` can occur. This change fixes them either to rearrange things so they can't happen or to throw a more specific exception.