I am trying to find duplicate words in a string array.
Here is my code for the comparison:
for ( int j = 0 ; j < wordCount ; j++)
{
NullPointerException means that one of your array members is not set (i.e. it is null)
Don't use == to compare strings.
You are on the right track - chances are stringArray[] contains some members that are not set. Eacy fix is to null check before using the values.
for ( int j = 0 ; j < wordCount ; j++)
{
for (int i = wordCount-1 ; i > j ; i--)
{
String wordi = stringArray[i];
String wordj = strinArray[j];
// If both are null it won't count as a duplicate.
// (No real need to check wordj - I do it out of habit)
if (wordi != null && wordj != null && wordi.compareTo(wordj) == 0 && i!=j)
{
//duplicate
duplicates++;
}
}
}
wordCount -= duplicates;
System.out.print("\nNumber of words, not including duplicates: " + wordCount);