Exception in thread "main" java.lang.NullPointerException
Hi,
I am trying to compare 2 strings using the "compareTo" function of String class in the main function of the following program.
Can anyone please tell me what might be going wrong ?
Here is the code:
public String[] readWordList() throws IOException
{
String wordList[] = new String[178691];
int j =0;
try{
BufferedReader br = new BufferedReader(new FileReader("tw106.txt"));
StringTokenizer st = null;
while((br.readLine()) != null)
{
for(int i=0; i<178691; i++)
{
wordList[i] = br.readLine();
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Error " +e);
}
catch (IOException e)
{
System.err.println("Error " +e);
}
return wordList;
}
//Function to read the input file
public String[] readInput(String s) throws IOException
{
String str, words[] = null;
int i=0;
StringTokenizer token = null;
try
{
FileReader fr = new FileReader(s);
BufferedReader br = new BufferedReader(fr);
do
{
str = br.readLine().trim();
words = new String[wordCount(str)];
token = new StringTokenizer(str);
while(token.hasMoreTokens())
{
words[i] = token.nextToken();
i++;
}
}while(br.readLine() != null);
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found!");
}
return words;
}
//Function to count number of words in a file
private static int wordCount(String line){
int numWords = 0;
int index = 0;
boolean prevWhiteSpace = true;
while(index < line.length())
{
char c = line.charAt(index++);
boolean currWhiteSpace = Character.isWhitespace(c);
if(prevWhiteSpace && !currWhiteSpace)
{
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
return numWords;
}
public static void main(String[] args) throws IOException {
String wordList[], words[];
breathalyzer b = new breathalyzer();
wordList = b.readWordList();
words = b.readInput(args[0]);
for(int i = 0; i<=words.length ; i++)
for(int j = 0; j<=wordList.length; j++)
{
words[i].equals(wordList[j].toLowerCase()); <------throws NullPointerException
}
}