title: Null pointer exception while checking if a string is present in a string array in android?
body: <p>I have an application which requires to check if a String is present in an array of String type, before adding it so as to avoid duplication. To do this, I wrote the following function:</p>
  
  <pre><code>public boolean arrayHas(String[] arr, String str)
  {
      for(int i=0;i&lt;arr.length;i++)
      {
          if(arr[i].equals(str))
              return true;
      }
      return false;
  }
  </code></pre>
  
  <p>To invoke this function, I'm using:</p>
  
  <pre><code>if(!arrayHas(contacts,str))
  {
      contacts[i] = str;
      i++;
  }
  </code></pre>
  
  <p>contacts and str are declared as follows</p>
  
  <pre><code>public static  String contacts[] = new String[]{};
  String str = "";
  Bundle bun = getIntent().getExtras();
  str = bun.getString("key");
  </code></pre>
  
  <p>Elements are added to 'contacts' only through the main code, it is empty at the beginning. I tried adding a toast to display the value of 'str' received through the intent and it works fine. But I'm getting a NullPointerException in the 'if' statement in the arrayHas function. Could someone help me out?</p>