When a variable can be null, check it before using it. Fixes #1308.