title: Java NullPointerException check on chained methods
body: <p>I want to check for a null pointer when accessing a field several classes deep (in a chain of get methods). However, if one of the earlier methods is <code>null</code> I get a <code>NullPointerException</code> anyways.</p>
  
  <p>This is what I want to check, though it can still get a <code>NullPointerException</code>:</p>
  
  <pre><code>if(x.getLocation().getBuilding().getSolidFuelInd() != null)
              pol.setWood_heat_ind(x.getLocation().getBuilding().getSolidFuelInd() ? "Y" : "N");
  </code></pre>
  
  <p>This the behavior I want the above code to exhibit:</p>
  
  <pre><code>if(x.getLocation() != null)
      if(x.getLocation().getBuilding() != null)
          if(x.getLocation().getBuilding().getSolidFuelInd() != null)
              pol.setWood_heat_ind(x.getLocation().getBuilding().getSolidFuelInd() ? "Y" : "N");
  </code></pre>
  
  <p>The field on the pol is optional and should only be set if the above getter is not <code>null</code>. However the building and location objects could also be <code>null</code>, so now I must check to that they're valid.</p>
  
  <p>Is there any sort of shorter way to check all the above like I want?</p>