[thelist] Friday Freebies, part II

Chris Danek Chris.Danek at cmgisolutions.com
Thu Jul 20 17:22:24 CDT 2000

<tip author="Chris Danek [cdanek at bdsinc.com]" type="Java">
	Don't chain java functions. You are just asking for nullpointer exceptions:

	if (someObject.getName().getAge().add().giveRaise().isStupid())

	Object o = someObject.getName();
		//you should check it for null here if it's going to happen
	Object o2 = o.getAge();
	Object o3 = o2.add();
	Object o4 = o3.giveRaise();
	if (o4.isStupid())

Trust me on this now. It's longer but it will catch -all- your nullpointer
exceptions. This is good practice in the long run, and will make other
people think you are good when they look through your code (and coversely,
curse at you when you don't).


More information about the thelist mailing list