setText of a TextView array <p>What I'm trying to do is have an EditText where people enter their names. As people press the "Add Player" button, the name which they just typed appears below in the list form. So I've created 8 textviews which are initially invisible but as people type the name press the "Add Player" button, the text changes to their names and becomes visible.</p> <p>So I set up a TextView array of the list of names, which are all text views</p> <pre><code>TextView[] nameList = new TextView[]{name1, name2, name3, name4, name5, name6, name7, name8}; </code></pre> <p>Later on in the code in the onClick section, I had</p> <pre><code>for (int i = 0; i &lt; 8; i++) { String name = etName.getText().toString(); nameList[i].setText(name); nameList[i].setVisibility(View.VISIBLE); } </code></pre> <p>However, with this, whenever I press the "add player" button, the app crashes and I get a NullPointerException. How would I solve this?</p> <p>The problem isn't with the for loop as the app crashed without it. The problem seems to be with the array as if I put</p> <pre><code>name1.setText(name); name1.setVisibility(View.VISIBLE); </code></pre> <p>the code worked fine.</p>