Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: need help troubleshooting a word counter program

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    Default need help troubleshooting a word counter program

    Here's the assignment:
    As demonstrated in the example run above, an external text file name is entered first. Then any number of individual words are entered, all lower case, and separated by spaces. After doing its analysis, the program then displays a chart that gives the frequencies in the text of the indicated words, to four decimal places. Thus 2.42% of all of the words in the Heart of Darkness text file are "I", 1.33% are "he", and so forth. Pretty clearly Heart of Darkness is narrated in the first person, and is mostly about men.

    Further requirements and tips:

    * you MUST use an array of WordCount objects to keep track of the occurrences of the indicated words.

    * Use printf from Chapter 5 to format your output.

    * If s is a String, then this String class method call:
    s.split(" "); // this is a single space surrounded by quote marks

    I'm really close to finishing it but I keep getting a java.lang.NullPointerException error on line 27. Also, I didn't feel the need to post the echo class or driver class because they are pretty straight forward. Here's the wordCount class that he gave us:

    public class WordCount{

    private String word;
    private int count;

    public WordCount(String w){
    word = w;
    count = 0;

    public String getWord(){
    return word;}

    public int getCount(){
    return count;}

    public void incCount(){count++;}

    public String toString() {
    return(word + " --- " + count);

    public boolean equals(Object other){
    WordCount i = (WordCount)other;
    return (this.word.equals(i.word));

    Here's what I have so far:

    import java.io.IOException;
    import java.util.*;

    public class WordFreq extends Echo{
    private String wordString;
    private int ct = 0;
    private String total = "";
    private WordCount[]words = new WordCount[1000];
    private String[]count;
    private String[]wordString2;
    private String s;

    public WordFreq(String f, String w)throws IOException{
    wordString = w;

    public void processLine(String line){
    wordString2 = wordString.split(" ");
    s = line;
    total += s + " ";
    count = total.split(" ");
    for(int j = 0; j < count.length; j++){
    words[j] = new WordCount(count[j]);
    for(int j = 0; j < wordString2.length; j++){
    if(wordString2[j].equals(words[j].getWord())){ //

    public void reportFrequencies(){
    for(int j = 0; j < wordString2.length; j++){
    System.out.println(wordString2[j] + words[j].getCount()); // USE PRINTF HERE ONCE IT WORKS

    EDIT: Just realized my if statement in the processline method doesn't look through all the words in the text. I'm thinking I'll have to create a for loop instead a while loop.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,471 Times in 2,422 Posts

    Default Re: need help troubleshooting a word counter program

    getting a java.lang.NullPointerException error on line 27.
    There is a variable with a null value on line 27. Look at line 27 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 27 and print out the values of all the variables on that line.

    Please edit your post and wrap your code with code tags:
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. troubleshooting GPA Calculator
    By uswhovian in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 18th, 2013, 04:23 PM
  2. Word Scramble Program Help
    By beginneratjava in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 18th, 2012, 12:24 PM
  3. [SOLVED] Need advice in troubleshooting
    By javaneedhelp in forum Exceptions
    Replies: 3
    Last Post: November 15th, 2011, 08:57 AM
  4. pls help me with word occurances program
    By kashif in forum Collections and Generics
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  5. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM