2012/3/20 Jacques Le Roux <jacques.le.r...@les7arts.com>: > BTW, this shows how stupid is the for loop in Java implementation. The > suggested safeList() should be handled by the compiler IMO, I > see no gains to not have it in but to get NPEs. Did I miss something? > Nothing. Is there an implementation of this safeList() in commons ?
I think I will keep the while as they are, and work only on those which only have the .hasNext(). Thanks for your comments ! > Jacques > > From: "Paul Foxworthy" <p...@cohsoft.com.au> > >> Hi Erwan, >> >> To be sure there is no Null Pointer Exception, yes, you need to test for >> null first. One possibility is to just let the NPE happen. >> >> The discussion at >> >> http://stackoverflow.com/questions/2250031/null-check-in-an-enhanced-for-loop >> >> suggests >> >> for( Object o : safe( list ) ) { >> // do whatever >> } >> >> Where safe would be: >> >> public static List safe( List other ) { >> return other == null ? Collections.EMPTY_LIST : other; >> } >> >> Cleaner code. I suspect the method would be inlined by most Java >> compilers. >> >> Cheers >> >> Paul Foxworthy >> >> >> Erwan de FERRIERES-3 wrote >>> >>> >>> Hi, >>> >>> I'm trying to remove a lot of iterators, and use the for-each syntax, >>> which exists since java 1.5. >>> During my journey, I found a lot of double tests for a while like this >>> one: >>> >>> while (typePurposes != null && typePurposes.hasNext()) { >>> (ContactMechWorker.java line 606) >>> >>> Can it be simplified to for(GenericValue contactMechTypePurpose : >>> theList) ? Or should I keep it like it is ? >>> >>> Regards, >>> >>> -- >>> Erwan de FERRIERES >>> www.nereide.biz >>> >> >> ----- >> -- >> Coherent Software Australia Pty Ltd >> http://www.cohsoft.com.au/ >> >> Bonsai ERP, the all-inclusive ERP system >> http://www.bonsaierp.com.au/ >> >> -- >> View this message in context: >> http://ofbiz.135035.n4.nabble.com/loop-code-simplification-tp4487741p4488324.html >> Sent from the OFBiz - Dev mailing list archive at Nabble.com. -- Erwan de FERRIERES