I agree with the statement from @Marc in the comments. I had the same issue and was able to solve it by catching the error in getEntity and initializing the key fields of er_entity.
getEntity
er_entity