added more null checks in sql queries