Add a null check for connection