Throw meaningful exception while referencing non-object

Fixes #850