So I have a prompt to create a class with an object that holds three values, one for a character ('me'), byte ('myascii') and int ('count'). These are private variables in the class so I have constructed setter and getter functions for all three, each independently. I am to then make another class with a function that creates an array of these variables, called Glyph per the name of the first class, and scan a text file to then sort the characters in it. If a character has not been found yet, an object is created for that character,byte and count and immediately added to the array. If the character has been found already, the count value of the object holding it, wherever it may be in the array, is to be incremented.
I have constructed two classes to accomplish this but I am getting a null pointer error when reading in:
char readGlyphChar = glyphObj[w].getMe(); //test to see if char exists in glyph
The Glyph class has standard getter and setter functions. The main file is as follows:
import java.util.Scanner;
//included libraries
public class Lab5_Main {
public static void main(String[] args)
{
Glyph glyphObj[] = new Glyph[512]; //create new object array 512 spaces long
File testerfile = new File("lab5testfile.txt"); //read in text file
Scanner inputText = new Scanner("lab5testfile.txt"); //tie text file to scanner
while(inputText.hasNext())
{ //while there is more than one line left in scanned text
String currentLine = inputText.nextLine(); //create a string and set it to current line of text
int p = currentLine.length(); //create count value p that is the num of chars in current line
int j; //create j count value
int w; //create w count value
for(j=0; j<=p; j++)
{ //repeat for as many times as there are chars in current line
char currentChar = currentLine.charAt(j); //create char to hold current char being read
byte currentByte = (byte)currentChar; //create current byte which is current char as byte
for(w=0; w<=126;w++)
{ //for all possible ascii symbols, numbers and letters
char readGlyphChar = glyphObj[w].getMe(); //test to see if char exists in glyph
if (currentChar == readGlyphChar)
{
int tempCount = 1;
tempCount = glyphObj[w].getCount();
tempCount++;
glyphObj[w].setCount(tempCount);
}
else if (currentChar != readGlyphChar)
{
int firstCount = 1;
glyphObj[w].setMe(currentChar);
glyphObj[w].setMyascii(currentByte);
glyphObj[w].setCount(firstCount);
}
}
}
}
System.out.println("Character Ascii Frequency\n");
int i;
for(i=0;i<=126;i++)
{
System.out.println(glyphObj[i].getMe() + glyphObj[i].getMyascii() + glyphObj[i].getCount());
}
}
}