Emma Hyde
Emma Hyde

Reputation: 23

java.lang.NullPointerException, can't tell why?

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?

Upvotes: 2

Views: 557

Answers (2)

Bert F
Bert F

Reputation: 87603

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.

Upvotes: 4

Reimeus
Reimeus

Reputation: 159864

Initialize the array wcArray prior to assigning any values:

wcArray = new WordCount[wordString.length];

Upvotes: 6

Related Questions