Add validation to prevent NullPointerException