added null check and exception throwing