Here is the latest changes with refactoring. It successfully builds, but NullPointerException still takes place