2

I'm writing a program that takes a series of words separated into tokens and reads a text, then counts their frequencies. This code has several related codes (WordCount, FreqStudy, and Echo) but the WordFreq class is what is causing the java.lang.NullPointerException error. Here is my code for WordFreq:

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

public class WordFreq extends Echo{

  WordCount[] wcArray;
  int ct;
  String[] sentence;
  double freq;
  String newLine;

  public WordFreq(String f, String words) throws IOException{
    super(f);
    String[] wordString = words.split(" ");
    for(int i=0; i<wordString.length; i++)
      wcArray[i] = new WordCount(wordString[i]);
    Scanner scan = new Scanner(new FileReader(fileName));
  }

  public void processLine(String line){
    newLine = line.toLowerCase();
    sentence = newLine.split(" "); 
    for(String s: sentence){
      for(WordCount w: wcArray){
        if(w.getWord().equals(s))
          w.incCount();
      }
      ct++;
    }     
  }

  public void reportFrequencies(){
    for (int i=0; i<wcArray.length; i++){
      freq = (wcArray[i].getCount() / ct); 
      System.out.print(wcArray[i].getWord()+" ");
      System.out.printf("%6.4f\n", freq);
    }
  }
}

And the error I recieve looks like this when I give it a file to read through the FreqStudy main class:

java.lang.NullPointerException
    at WordFreq.<init>(WordFreq.java:17)
    at FreqStudy.main(FreqStudy.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

I really don't understand what the issue is and I've been working on this all day so I feel brain dead. Please take a look if you can, I would really appreciate it.

EDIT: I realized that I didn't initialize it right after posting this, but my frequencies return 0 results. Anyone help?

3
  • It's returning 0, and get count is returning 0 as well Commented Apr 16, 2013 at 23:50
  • Initiate double ct = 0.0 or 1.0 and debug your code why getCount returning 0. Commented Apr 16, 2013 at 23:52
  • @EmmaHyde If debugging does not work, create another post including the WordCount class and how the methods in WordFreq are called. Commented Apr 17, 2013 at 0:46

2 Answers 2

6

Initialize the array wcArray prior to assigning any values:

wcArray = new WordCount[wordString.length];
Sign up to request clarification or add additional context in comments.

Comments

4

You need to examine the stacktrace carefully:

java.lang.NullPointerException
    at WordFreq.<init>(WordFreq.java:17)

Around line 17 of WordFreq (WordFreq.java:17), which is in a constructor of the WordFreq class (WordFreq.<init>), something is getting used or dereferenced while it is null.

wcArray[i] = new WordCount(wordString[i]);

Likely candidates: wcArray and wordString. Now make sure those objects/arrays got initialized. wordString did via the return value of split(), but wcArray did not. Arrays need to be allocated (wcArray) or assigned to a pre-existing array (wordString):

String[] wordString = words.split(" ");
wcArray = new String[wordString.length]
for(int i=0; i<wordString.length; i++)
  wcArray[i] = new WordCount(wordString[i]);

Further considerations: One might think that wcArray[i] might be null and cause the NPE, but since it the target of an assignment, it wouldn't cause a NPE - the null value would be overwritten with the incoming assignment.

Similarly, one might think wordString[i] might be null and cause the NPE, but this would cause a null to be passed to a method, which is fine. If the WordCount constructor had an problem with a null being passed to it, then the stacktrace would include a WordCount entry above the at WordFreq.<init> entry.

1 Comment

Hello, I am having a different problem now. All of my frequencies are returning as 0. Do you have any idea why this may be happening?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.