Null check chain vs catching NullPointerException
问题 A web service returns a huge XML and I need to access deeply nested fields of it. For example: return wsObject.getFoo().getBar().getBaz().getInt() The problem is that getFoo() , getBar() , getBaz() may all return null . However, if I check for null in all cases, the code becomes very verbose and hard to read. Moreover, I may miss the checks for some of the fields. if (wsObject.getFoo() == null) return -1; if (wsObject.getFoo().getBar() == null) return -1; // maybe also do something with