Added a test and fix for a potential NPE