NullPointerException handling and code formatting