added try catch and fixed the error in referencing variables