Prevent NPEs in the code

The PR fixes possible NPE errors discovered by FindBugs