fix a potential npe
new test for #997