fix NPE #597

fix the #597 issue (a test has been created to be able to reproduce the issue)