adding some conditional structures to avoid nullPointers