title: When is it OK to catch NullPointerException?
body: <p>Effective java recommends that we shouldn't <code>catch</code> <code>NullPointerException</code>.
  Is it always right?</p>
  
  <p>In many cases of catching <code>NullPointerException</code>, catch body only calls <code>printStackTrace()</code>.</p>
  
  <p>If I don't catch <code>NullPointerException</code> and call <code>printStackTrace()</code>, how I can check the place where the <code>exception</code> occurred?</p>
  
  <p>And also if I catch <code>NullPointerException</code> and the <code>catch</code> body is empty, we cannot get any stack information at that time, can we?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I analyzed statistics of catching RuntimeException in google android source, AOSP4.2.2_r1.2.</p>
  
  <p>There are 249 RuntimeException catchings and followings are statistics of catch-body.</p>
  
  <pre><code>42%: throw it again as other Exceptions (RuntimeException: 33%, others: 8%)
  
  32%: just return null or 0/false/true or other default values
  
  14%: just call log or printstacktrace
  
  5%: just comment like "// Fall through.", "// ignore, not a valid type.", "// system process dead", "// do nothing"
  
  2%: empty body
  
  3%: display error messages or send fail codes (ex. network service discovery failed: replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED, NsdManager.FAILURE_INTERNAL_ERROR); )
  
  
  3%: intialize or reset related variables.
  </code></pre>
  
  <p>In almost cases, dalvik and external handle NPE by throwing other Exceptions.</p>
  
  <p>But frameworks usually handle by returning null or other values.</p>
  
  <ol>
  <li><p>Do you think that throwing other Exceptions in catch-body is not bad or good handling?</p></li>
  <li><p>If NPE occured in higher levels(ex. applications) and the developer confirms the exception is not critical because the app is fully independent, can I accept for our developer to ignore the NPE by catching?</p></li>
  </ol>
  
  <p>and one more thing, </p>
  
  <p>Can I conclude that google android framework source code may be some unstable in aspects of <code>RuntimeException</code>?</p>