java - Is there a shortcut to execute something only if its not null? -


this question has answer here:

i find myself writing statement

myobject myobject = something.getthatobject(); if( myobject !=null &&     myobject .somebooleanfunction()){  } 

in order prevent null pointer exception. there shortcut in java? i'm thinking myobject..somebooleanfunction()?

in java 8:

static <t> boolean notnull(supplier<t> getter, predicate<t> tester) {     t x = getter.get();     return x != null && tester.test(x); }      if (notnull(something::getthatobject, myobject::somebooleanfunction)) {         ...     } 

if style new readers, 1 should keep in mind, full functional programming bit nicer.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -