Add null reference check (fixes #226)