Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is impossible. If you think about it for a while, you'll realize that that is also a nonsense. Take this code for example:</p> <pre><code>String s = getSomeString(); // assigns null int length = s.length; // throws NPE if (length &gt; 10) { // what would you do here?? length is un-initialized domeSomething(); } </code></pre> <h2>Edit</h2> <p>Now that you've updated you question with a code snippet, there are two <strong>general</strong> approaches to deal with <code>null</code>s:</p> <h2>1. Disallow them where they aren't supposed to be</h2> <p>In your code snippet, you've provided a list of <code>EditText</code>s, where any o them might be null. Well, in my opinion if you have an <code>Activity</code> in Android which should have some Views, then it is a <strong>programming error</strong> if any of them is <code>null</code> for whatever reason. An activity without some of its views can't work properly, so throwing a NPE and exiting the application is a perfectly sensible thing to do.</p> <p>The NPE tells you "<em>hey, you've made a mistake and you have to fix it</em>". Until then, it is not safe for the app in an unpredicted state to continue execution, because no one knows what can happen, what data can become corrupted.</p> <h2>2. Allow them where they make sense, and provide null-checks</h2> <p>There are some places where you can have variables whose value can be empty. There are several approaches how to express an empty value and <code>null</code> is the most obvious one.</p> <p>In that case, you can't just ignore the possibilty of a NPE and you have to do some null-checks. But again, a <strong>NPE is not your enemy</strong>. It will tell you that you've forgotten to account for all the possible situations that you should have.</p> <p>Of course, repeated null checks make the code cluttered and noisy, so you can create some methods that generalize the null checks for you. I could not come up with a sensible example, so take this method only for illustration:</p> <pre><code>public void clearEditTexts(EditText... editTexts) { for (EditText e : editTexts) { if (e != null) { e.setText(""); } } } </code></pre> <p>This method clears the contents of all the passed EditTexts. Any of them can be null, but this method does these null-checks internally, so you don't have to do it manually for each of them.</p> <hr> <p>This question is also a very good reading about handling nulls: <a href="https://stackoverflow.com/questions/271526/avoiding-null-statements-in-java">Avoiding != null statements</a></p>
    singulars
    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. 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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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