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!

If you post with code=java tags we may be able to see which line is line 180 where the problem happens (or maybe you can just tell us which line it is). Anyway, on the line you have either an uninitialised variable or a variable that has been set to null, probably by a previous method call that failed.

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];

Don't you think something is wrong with the red section? You tell your application to open file for reading. You request to read file while there is some token to read. After this you increment accumulator and declare Array. Repeat this two times and more and you effectively overwrite what ever was originally stored in that array. There are basically two solutions:

  1. You find out how many entries you will be reading and based on that declare size of the array.
  2. Read data in some Collection, which are able to adjust their size dynamically Vector, ArrayList etc.

Before we continue further on let me know if you been touched anything else beside Array. Something like ArrayList, List or Vector so we do not waste the time...

Thanks for the assist! I was getting wrapped around the axle with all these array declarations.

Whats wrong with it?

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];

Don't you think something is wrong with the red section? You tell your application to open file for reading. You request to read file while there is some token to read. After this you increment accumulator and declare Array. Repeat this two times and more and you effectively overwrite what ever was originally stored in that array. There are basically two solutions:

  1. You find out how many entries you will be reading and based on that declare size of the array.
  2. Read data in some Collection, which are able to adjust their size dynamically Vector, ArrayList etc.

Before we continue further on let me know if you been touched anything else beside Array. Something like ArrayList, List or Vector so we do not waste the time...

Didn't you read his post? He explains what is wrong.

Ok well im working on the same program for school. So he needs to set the accummulator to a certain number
like array1 = new int[25];

He was creating a new array every time he read a line. He needed to pre-size the array large enough to hold all of the entries before he began filling it or use an unbounded collection like ArrayList.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.