title: NullPointerException from Boolean
body: <p>This is one for the java purists I think. I recently had an issue with a method to perform a custom parsing of String values to a Boolean. A simple enough task, but for some reason the method below was throwing a NullPointerException in the null case...</p>
  
  <pre><code>static Boolean parseBoolean(String s)
  {
      return ("1".equals(s) ? true : ("0".equals(s) ? false : null));
  }
  </code></pre>
  
  <p>The return type for the method is Boolean so why or how can a NullPointerException be thrown?
  From debugging through it seems the exception is being thrown at the point where the nested in-line conditional statement evaluates to null and returns null to the outer in-line conditional, but again I can't explain why.</p>
  
  <p>Eventually I gave up and rewrote the method as follows, which works as expected:</p>
  
  <pre><code>static Boolean parseBoolean(String s)
  {
      if ("1".equals(s)) return true;
      if ("0".equals(s)) return false;
  
      return null;
  }
  </code></pre>
  
  <p>The following code is half way between the two and also works as expected:</p>
  
  <pre><code>static Boolean parseBoolean(String s)
  {
      if ("1".equals(s)) return true;
  
      return "0".equals(s) ? false : null;
  }
  </code></pre>