title: how to compare elements in a string array in java?
body: <p>I am trying to find duplicate words in a string array.</p>
  
  <p>Here is my code for the comparison:</p>
  
  <pre><code>   for ( int j = 0 ; j &lt; wordCount ; j++)
     {    
         for (int i = wordCount-1 ; i &gt; j ; i--)
         {       
             if (stringArray[i].compareTo(stringArray[j]) == 0 &amp;&amp; i!=j)
             {
                 //duplicate
                 duplicates++;
             }
         }
     }
     wordCount -= duplicates;
     System.out.print("\nNumber of words, not including duplicates: " + wordCount);
  </code></pre>
  
  <p>in the if statement, it says <code>NullPointerException</code>. What does this mean? Is there a better way to do this? I tried simply doing </p>
  
  <pre><code>if (stringArray[i] == stringArray[j] &amp;&amp; i!=j)
  </code></pre>
  
  <p>but that kept giving me wrong answers.</p>