[ref] Added some nullptr checks and made tweaks to error message generation.