Fixed null pointer exception

also added a try/catch block