Added null check in query