0

I'm making a project to go through a text file and output a tally of each letter in the file.

import java.util.*;
import java.io.*;
    public class frequencyAnalysis {

        private static String text;
        public static String alphabet;
        public static int Freq[];

    public frequencyAnalysis(String text) {
        this.text = text;
        int [] Freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  //array of ints to keep track of how many of each letter there is.
        alphabet = "abcdefghijklmnopqrstuvwxyz";  //point of reference for the program to know which number in the array should be increased
    }

    public static void freqAnalysis() throws IOException {

        String token = "";
        int index;

        File subset = new File(text);  //creates a new file from the text parameter
        Scanner inFile = new Scanner(subset);

        while(inFile.hasNext()) {
            token = inFile.next();
            index = alphabet.indexOf(token);
            if (index == -1) {  //makes sure that the character is a letter
                break;  
            } else {
                Freq[index]++;
            }
        }
        inFile.close();
    }
 }

This is the class that's supposed to go through a given text file, and count how many of each letter there is in it.

import java.util.*;
import java.io.*;
public class tester {
    public static void main(String args[]) throws IOException {
        Scanner in = new Scanner(System.in);

        System.out.println("Please type the input file path: ");  //allows the user to specify a file
        String input = in.next();

        frequencyAnalysis Freq = new frequencyAnalysis(input);

        frequencyAnalysis.freqAnalysis();  //calls the method to run through the file

        for(int i = 0; i <= 25; i++){  //prints the alphabet and the Freq array
            System.out.println(frequencyAnalysis.alphabet.charAt(i) + ": " + frequencyAnalysis.Freq[i]);  //this is where the error is
        }

    }
}

This is the implementation class, which allows the user to specify a file and then runs the freqAnalysis method to adjust the static Freq array, which is then printed. However, when I run the program it gives me a java.lang.NullPointerException error on the specified line. I've already figured out that the problem is in "frequencyAnalysis.Freq[i]", not in "frequencyAnalysis.alphabet.charAt(i)". However, I don't know what the problem is or how to fix it.

Ethan Stedman
  • 11
  • 1
  • 6
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Thiyagu Jul 24 '18 at 17:36

4 Answers4

0

In the constuctor you create an initialize local variable int [] Freq instead of class field. Instead of:

int [] freq = {0, 0, 0, 0, 0, 0, ...

you should have:

freq = {0, 0, 0, 0, 0, 0, ...
piradian
  • 414
  • 7
  • 19
0

Ok so you aren't initializing the array correctly in your frequencyAnalysis class.

public static int Freq[];

you think you're initializing it with int [] Freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

but in reality it should be: this.Freq = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

scigs
  • 529
  • 1
  • 5
  • 12
0

The other answers are decent, but have some flaws in coding style.

First of all, integer arrays default to initialize with zeroes, so you can simplify your initialization of Freq. Also, you created a local variable Freq inside of your constructor for frequencyAnalysis instead of setting the static class variable.

You should change your constructor to this:

    public frequencyAnalysis(String text) {
        this.text = text;
        frequencyAnalysis.Freq = new int[26];  //array of ints to keep track of how many of each letter there is.
        alphabet = "abcdefghijklmnopqrstuvwxyz";  //point of reference for the program to know which number in the array should be increased
    }

Also, class names should always be capitalized! (Change it to FrequencyAnalysis)

agillgilla
  • 859
  • 1
  • 7
  • 22
  • I've always capitalized single word variables, but for multiple word variables I don't capitalize the first word, but then capitalize the rest (like "userInput" or "thisVariable".) – Ethan Stedman Jul 24 '18 at 17:57
  • I wasn't talking about variables, I'm saying that class names should be capitalized and in CamelCase. According to the [Documentation](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#15411): "Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive..." – agillgilla Jul 24 '18 at 18:20
0

To let you know that is happening,

public frequencyAnalysis(String text) { this.text = text; int [] Freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //array of ints to keep track of how many of each letter there is. alphabet = "abcdefghijklmnopqrstuvwxyz"; //point of reference for the program to know which number in the array should be increased }

When you do this int [] Freq is a new local variable the scope is limited to constructor and at the other side public static int Freq[]; is still Null, point to same variable for initialization, as stated above by others

Vikram
  • 391
  • 1
  • 3
  • 7