handle NPE gracefully
(cherry-picked from 83633f8)