Results 1 to 5 of 5
  1. #1
    puppysnlove is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Question Need help with program that has null exception in main

    This is my program
    The error happens during running the application after counting words.
    My teacher says I am calling an unwritten array but I have no idea how.
    please help :o


    import java.util.Scanner;

    import java.io.*;

    public class WordProcessing
    {
    public static void main (String [] args) throws IOException
    {

    Scanner keyboard = new Scanner(System.in);

    String fileName;
    Word [] list = new Word[100];
    int wordCount;
    int allWords;

    String [] array = new String[100];


    System.out.println("Word Processing Application");

    System.out.println("Enter the name of the text file ");
    fileName = keyboard.nextLine();

    Scanner inFile = new Scanner(new File(fileName));

    wordCount = count(fileName, list, array);

    allWords = totWords(list, fileName);


    System.out.println(avgOccurences(list, allWords) + " is the average occurence of a word");

    System.out.println(count(fileName, list, array) + " is how many words are in the array");

    System.out.println(avgLength(list, allWords) + " is the average length of a word");

    System.out.println(mostCommon(list, allWords) + "is the most common word/words");

    System.out.println(totWords(list, fileName) + " is the total amount of words in the file");

    findSmallest(list, wordCount);
    sort(list, wordCount);

    int count;
    count = 0;

    while (count < allWords)
    {
    System.out.println(list[count]);
    count++;
    }
    } //end main

    /************************************************** ***********************
    // Method count: This method will assign each part of array to a seperate
    // Word objects calling the Word class.
    ************************************************** ***********************/


    static int count(String fileName, Word list[], String array[]) throws IOException
    {
    Scanner inFile = new Scanner(new File(fileName));

    int wordCount;
    String line;
    int count2 = 1;
    int searchReturn;
    int count3;
    int allWords;

    // read the first line in the file
    allWords = totWords(list, fileName);



    System.out.println("Counting Words");



    wordCount = 0;
    line = inFile.nextLine();

    while (inFile.hasNextLine()) // while not end of file
    {



    if (wordCount < 100)
    {
    searchReturn = search(list, wordCount, line);

    if (searchReturn == -1)
    {
    list[wordCount] = new Word(line);
    line = inFile.nextLine();
    wordCount++;
    }
    else
    {
    list[searchReturn].incrementCount();
    line = inFile.nextLine();
    }
    }
    else
    {
    System.out.println("Maxed 100 unique words");
    }


    }
    findSmallest(list, wordCount);
    sort(list, wordCount);
    return wordCount;



    } //end count

    /************************************************** ************
    // Search Method: This method searches the array to see if the
    // string is equal to another string.
    ************************************************** ************/

    static int search(Word list[], int wordCount, String Key) throws IOException
    {
    int line;
    int count = 0;
    line = 0;
    String word;

    int pos = 0;

    word = list[pos].getWord();

    while (pos < wordCount && word.compareTo(Key) < 0 || word.compareTo(Key) > 0)
    pos++;

    if (pos == wordCount)
    pos = -1;

    return pos;
    } //end search


    /************************************************** ***********
    // Method totWords: counts the total number of words in the
    // file.
    ************************************************** ***********/
    static int totWords(Word list[], String fileName) throws IOException
    {
    Scanner inFile = new Scanner(new File(fileName));

    int totWords;
    String line;
    totWords = 0;

    line = inFile.nextLine();

    while (inFile.hasNextLine())
    {
    totWords++;
    line = inFile.nextLine();
    }

    return totWords;


    } //end totWords

    /************************************************** **********
    // Method findSmallest: this method finds the word object
    // that is highest in alphebetical order.
    ************************************************** **********/

    static int findSmallest(Word list[], int wordCount)
    {
    int start = 0;
    int loc = start;
    for (int pos = start + 1; pos <= wordCount; pos++)
    if (list[start].getWord().compareTo(list[start + 1].getWord()) < 0)
    loc = pos;
    return loc;


    }

    /************************************************** **********
    // Method sort: this method compares all the strings to
    // each other and switches them to put the array in
    // alphebetical order.
    ************************************************** **********/

    static void sort (Word list [], int wordCount)
    {
    Word temp;
    int position;

    System.out.println("Sorting Words");

    for (int loop = 0; loop < wordCount - 1; loop++)
    {
    position = findSmallest(list, wordCount);
    if (position != loop)
    {
    temp = list[position];
    list[position] = list[loop];
    list[loop] = temp;
    }
    }
    }

    /************************************************** ***********
    //
    ************************************************** ***********/

    static int avgLength (Word list [], int allWords)
    {
    int wordLength;
    int count;
    int addLength;
    int avgLength;
    count = 0;
    String word;
    addLength = 0;

    word = list[count].getWord();

    while(count < allWords)
    {
    wordLength = list[count].getLength();
    addLength = wordLength + addLength;
    count++;
    }

    avgLength = addLength / allWords;
    return avgLength;


    }
    /************************************************** *********
    //
    ************************************************** *********/

    static int avgOccurences (Word list [], int allWords)
    {
    int avgOcc;
    int wordOcc;
    int addOcc;
    int count;
    count = 0;
    addOcc = 0;

    while (count < allWords)
    {
    wordOcc = list[count].getCount();
    addOcc = addOcc + wordOcc;
    count++;
    }

    avgOcc = addOcc / allWords;
    return avgOcc;
    }

    /************************************************** **********
    //
    ************************************************** **********/

    static String mostCommon(Word list[], int allWords)
    {
    int count;
    int count2;
    int saveCount;
    int count3 = 0;

    int highCount = 0;

    count = 0;



    while(count < allWords )
    {
    count2 = list[count].getCount();

    if (count2 > highCount)
    {
    highCount = count2;
    count++;
    }

    else if(count2 < highCount)
    {
    count++;
    }

    else if(count2 == highCount)
    {
    count++;
    list[count2].mostCommon(highCount);
    }
    }

    System.out.println("The most common words in the list are:");
    while (count3 < allWords)
    {
    if(list[count3].commonCount() == highCount)
    {
    System.out.println(list[count3].getWord());
    count3++;
    }
    }


    return list[highCount].getWord();
    }
    }




    here is my Word class

    import java.io.*;

    class Word
    {
    private String word;
    private int count;
    private int count2;

    public String getWord()
    {
    return word;
    }

    public int getCount()
    {
    return count;
    }

    public Word(String line)
    {
    word = line;
    count = 1;
    }

    public int incrementCount()
    {
    count++;
    return count;
    }
    public int getLength()
    {
    int length;
    length = word.length();
    return length;
    }
    public int mostCommon(int highCount)
    {
    count2 = highCount;
    return count2;
    }
    public int commonCount()
    {
    return count2;
    }
    }
    Last edited by puppysnlove; 12-09-2010 at 09:33 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Could you please copy and post the exact runtime stack trace (the NullPointerException message and all the stuff that goes with it)? And indicate which line of your code it is referring to?

  3. #3
    puppysnlove is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default results

    after i type in the name of the text file it says

    Exception in threas "main" java.lang.NullPointException
    at WordProcessing.search<WordProcessing.java:147>
    at WordProcessing.count<WordProcessing.java:104>
    atWordProcessing.main<WordProcessing.java:40>

  4. #4
    puppysnlove is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    and I think it is line 90

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I think that the line causing the NullPointerException is

    Java Code:
    word = list[pos].getWord();

    which is line 134 of the code you posted. The stacktrace reports that it is line 147, but the code you posted is evidently not the code that results in the runtime message.

    Anyway, what this means is that something in that line is null when it shouldn't be. The candidates are list because you can't access an array if it is null. And list[pos] because you can't call a method like getWord() on something that is null.

    So the first thing to do is to print the values of these things and see which (if either) is null.

    Java Code:
    static int search(Word list[], int wordCount, String Key) throws IOException
    {
      int line;
      int count = 0;
      line = 0;
      String word;
    
      int pos = 0;
    
      System.out.println("list is " + list);
      System.out.println("list[pos] is " + list[pos]);
      word = list[pos].getWord();
      
      // etc

    Insert this debugging code and see if either list or list[pos] is null. If either are null you have to go back through your code (to count() and then to main()) to try and identify where you assigned a nonnull value to these things.

    ------------------------

    If you can find where you think you assigned something nonnull to list and put nonnull things into the list array (so that list[pos] shouldn't be null) then you have to figure out why that didn't happen. If you get stuck post back indicating where you thought nonnull assignments were made.

    If you cannot find such assignments, then that is where you problem lies: list must be assigned a nonnull value. And list must be filled with nonnull Word references.

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  2. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  3. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  4. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  5. Replies: 1
    Last Post: 08-06-2007, 10:11 AM

Posting Permissions

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