Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you given any thought to a <code>try{} catch(NullPointerException e){}</code> block? It might feel less elegant, but it will stop your code if any of the method calls fails because the previous one returned <code>null</code>, and it will give you the chance to give the default value if it is null.</p> <p>Another option would be something like this:</p> <pre><code>Something v = /*Default Value*/ // Will be overwritten if subsequent methods succeed. Object temp = a.getB(); // Use whatever Object type getB() returns. if(temp != null){ temp = temp.getC(); /* If getC() returns a different type of object, * either use a different variable or make the temp variable even broader * (such as the generic Object type) */ if(temp != null){ temp = temp.getD(); if(temp != null){ temp = temp.getE(); if(temp != null) v = temp; /* If all previous calls returned something substantial, * v will be something useful */ }//if(getE() != null) }//if(getD() != null) }//if(getC() != null) }//if(getB() != null) </code></pre> <p>If you want, you could use a slightly less CPU efficient, but easier to read, version by not nesting the if statements. If all of the if statements are executed after eachother, a single null will prevent all of the next statements from executing, although its value will still be checked every time.</p> <p>As far as generating these statements, I'm not really sure. That will really depend on how far in advance you can predict what new methods will be available from the <code>Object</code> returned by previous method calls. If you're aiming for auto-generation of code, you might be better off with my first suggestion: <code>try-catch</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload