Fix possible NPE

There was NPE in some cases.