fix (and test for) NPE when optimizing variables in path expressions;
introduced by recent bug fix