Hello, I have a class that pulls account numbers from valid_accounts.txt and is supposed to take each number and put it into an array. In my driver program, I should be able to call the method of the class that pulls the information and populate it into my array created in the driver program. Instead, I get this error:
Exception in thread "main" java.lang.NullPointerException
at ChargeAccount.getAccounts(ChargeAccount.java:180)
at Good_Brandon_Final_Charge_Account_Validation.main(Good_Brandon_Final_Charge_Account_Validation.java:55)
I attached the valid_accounts.txt file
Here is the code I have in the class:
import java.io.*; // needed for file classes
import java.util.Scanner; //needed for the Scanner class
public class ChargeAccount //MUST match the file name!
{
//FIELDS OF THE CLASS
private int[] checkAccounts; //Stores values inside accounts_to_check.txt.
private int[] validAccounts; //Stores values inside accounts_to_check.txt.
private int[] verifiedValid; //Stores values that were verified to be valid accounts.
//Constructor. No-arg constructor to create objects from the ChargeAccount class.
public ChargeAccount()
{
/*
//Allocate array1 values to validAccounts array.
validAccounts = new int[array1.length];
for (int index = 0; index < array1.length; index++)
validAccounts[index] = array1[index];
//Allocate array2 values to checkAccounts array.
checkAccounts = new int[array2.length];
for (int index = 0; index < array2.length; index++)
checkAccounts[index] = array2[index];
*/
}//end constructor
//MUTATOR METHODS
/*
getAccounts method to get accounts from valid_accounts.txt and accounts_to_check.txt and populate the data
from these files into the checkAccounts array and validAccounts array.
@param array1 the values that will be copied to validAccounts array
@param array2 the values that will be copied to checkAccounts array
*/
//Create public void getAccounts with the throws IOException clause at the end.
public void getAccounts () throws IOException
{
int[]array1;
int value1;
int[]array2;
int value2;
int accumulator = 0;
//Open valid_accounts.txt.
File file = new File("valid_accounts.txt");
Scanner inputFile = new Scanner(file);
//Read lines from the file until there are no more to read.
while (inputFile.hasNext())
{
accumulator++;
array1 = new int[accumulator];
//For each line read, store the value into an element in array 1.
for (int index = 0; index < array1.length; index++)
{
value1 = inputFile.nextInt();
array1[index] = value1;
//Copy the contents of array1 to validAccounts.
validAccounts[index] = array1[index];
}//end for loop
}//end while loop
//Close the file.
inputFile.close();
Here is the code I have in the driver program:
import java.io.*; // needed for file classes
public class Good_Brandon_Final_Charge_Account_Validation //MUST match the file name!
{
public static void main (String[ ] args) throws IOException
{
// Executables
System.out.println("Program results by Brandon Good for CSCI 2911-H.");
System.out.println("This program solves Final ChargeAccount Problem.");
//Create checkAccount array, validAccount array, and verifiedAccount array.
int[] checkAccount = new int[25];
int[] validAccount = new int[50];
int[] verifiedAccount;
//Instantiate an object of the ChargeAccount class.
ChargeAccount testCharge = new ChargeAccount();
//Call getAccounts method to pull info from files to arrays in ChargeAccount class.
testCharge.getAccounts();
//Return contents of checkAccounts from ChargeAccount class and copy contents to checkAccount.
validAccount = testCharge.returnCheckAccounts();
for (int index = 0; index < checkAccount.length; index++)
System.out.print(validAccount[index]);
Please help!