Hi,

I have been making a checkbook program for my final project. When I run my program and try to read a file into my array, it is giving me this error:

Exception in thread "main" java.lang.NullPointerException

It is pointing to this specific line:

balance = myRegister.alterRegister(checkBook.getAmount());

I'm not sure how to resolve this, I have been trying to research what exactly is the problem but I can't find it.


import java.util.*;
import java.io.*;

public class test {
     public static void main(String[] args)throws IOException {
	      
		  //Open new checkbook array
		  Check [] checkBook = new Check[100];
		  double balance = 1000.00;
		  int choice;
		  
	          Register myRegister = new Register();
		  System.out.println();
                  System.out.println("Welcome to Tommy's Check Register.");
		  System.out.println();
		  System.out.println();
                  Scanner input = new Scanner(System.in);
		  
		  //if checkbook exists, read from checkbook text file
		  System.out.print("If using existing checkbook enter 1, otherwise enter 3: ");
		  int existChoice = input.nextInt();
		  if (existChoice == 1) {
		       myRegister.readChecks(checkBook, "checkbook.txt");
				 for (int i=0; i < checkBook.length; i++) {
				      balance = myRegister.alterRegister(checkBook[i].getAmount());
				 }
		  } else if (existChoice == 3) {
		       checkBook = null;
		  }

something isn't initialised. That's all there is to it.
It's up to you to find out what that something is.

This article has been dead for over six months. Start a new discussion instead.