title: NullPointerException error while trying to remove a string word from an Array in a remove() method
body: <p>I'm making this method remove() which takes a String word as argument, to delete from a global Array "words", but I keep getting a NullPointerException for some reason I cannot find, been stuck for hours. </p>
  
  <p>Basically I check for if the word is in the first position, else if is in the last position, or else if it is in neither so I check all the array, and add the first half before the position of the word, and then add the second half after the position of the word in the array, as to skip it and "delete it". But I'm getting a NullPointerException in the for loop looking for the position of the word in the array. Code for the method is here:</p>
  
  <pre><code>public void remove(String a){
  
  String[] temp_arr = new String[words.length-1]; // make array with 1 less length for deleted 
  
      if(words[0].equals(a)){ // if the word is the first in the array
  
          for(int x=0, z=1; x&lt;temp_arr.length; x++,z++)
              temp_arr[x]=words[z];
  
          words = temp_arr;
  
      } else if(words[words.length-1].equals(a)){ // if the word is in the last position of the array
  
          for(int x=0, z=0; x&lt;temp_arr.length; x++,z++)
              temp_arr[x] = words[z];
  
          words = temp_arr;
  
      } else{ // if the word is in neither first or last position of array
  
          // THIS IS WHERE the exception is thrown, in this for loop, in the if(words[k].equals(a))
  
          int k=0;
          for (; k&lt;words.length; k++){ // find the position of the word to delete
              if (words[k].equals(a)) {
                  break;
              }
          }
  
          for (int i = 0; i &lt; k-1; i++){ // add first part of array before the word
  
              temp_arr[i] = words[i];
          }
  
          for(int c = k, b = k+1; c&lt; temp_arr.length; c++,b++){
              temp_arr[c] = words[b];
          }
  
          words = temp_arr; // assign the new values to global array
  
      }
  }
  </code></pre>
  
  <p>Also, if theres any suggestions for good coding practice would be appreciated, thanks!</p>
  
  <p>** I can only use Arrays as my data structure for this method.</p>