Results 1 to 3 of 3
- 08-15-2009, 07:42 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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
}
}
- 08-15-2009, 08:00 AM #2
NullPointerException
Null pointer error happens when u use a pointer whose value is null -or with being defined first-
So u better make this chek before using any pointer
if(.....!=null)
{
...
}HosHos :cool:
hoss.2011@hotmail.com
- 08-15-2009, 08:05 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
Exception in thread "main" java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 0Last Post: 02-24-2009, 01:19 PM -
Exception in thread "main" java.lang.NullPointerException
By Manfizy in forum New To JavaReplies: 1Last Post: 02-17-2009, 10:54 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks