Added check for not null, made error be an exception. Made error message be clearer.