Results 1 to 3 of 3
  1. #1
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Smile 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

    }


    }

  2. #2
    HosHos's Avatar
    HosHos is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default 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

  3. #3
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  2. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 02:19 PM
  4. Replies: 1
    Last Post: 02-17-2009, 11:54 AM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •