Getting NullPointerException in program
What I am trying to do is create a bank class (which uses an array to store the accounts) that has a create method which is used to create a new account in the bank. The method requires the user to input data (account number, name, phone number, and balance) and will create a new account in the bank, but the method has to check to see if the account number is already in use and if it is print an
error message and return (this is where I am having problems).
I have three files an Account class, a Bank class, and a BankDriver class, posted below are the three classes
Code Java:
public class Account
{
private final double RATE = 0.03; // interest rate of 3.0%
private long acctNumber;
private double balance;
private String name;
private String phoneNumber;
public Account ()
{
name = "";
acctNumber = 0000;
phoneNumber = "555-1111";
balance = 0.0;
}
//---------------------------------------------------------------------------------------------
// Creates account by defining the owner, account number, phone number, and starting balance
//---------------------------------------------------------------------------------------------
public Account (String owner, long account, String phone, double initial)
{
name = owner;
acctNumber = account;
phoneNumber = phone;
balance = initial;
}
//-----------------------------------------------------------------------
// Returns balance
//-----------------------------------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------------------------------
// Returns account number
//----------------------------------------------------------------------
public long getAcctNumber()
{
return acctNumber;
}
public String toString()
{
return acctNumber + "\t" + name + "\t" + phoneNumber + "\t" + balance;
}
}
Code Java:
public class Bank
{
private Account[] accounts;
private int count = 0;
public Bank()
{
accounts = new Account[30];
}
public void create (long accountNum, String owner, String phNumber, double initial)
{
if(accountNum != accounts[count].getAcctNumber())
{
accounts[count] = new Account(owner, accountNum, phNumber, initial);
count++;
}
else if(accountNum == accounts[count].getAcctNumber())
System.out.println("Account Number already exsists.");
}
public String toString()
{
String report = "";
for (int i = 0; i<count; i++)
report += accounts[i] + "\n";
return report;
}
}
Code Java:
public class BankDriver
{
public static void main (String[] args)
{
Bank bank1 = new Bank();
bank1.create (1111, "Bob", "555-1234", 100.00);
System.out.println(bank1);
}
}
The problem I believe is my create method but I am not sure of the best way to fix it. Any help would be greatly appreciated.
Re: Getting NullPointerException in program
You mention a NullPointerException in the title of your post, but no mention of it in your post. Is this the problem? Where is this exception being thrown (recommended you post the full stack trace)?
Re: Getting NullPointerException in program
Yes it does seem to be the problem as all three programs have no trouble compiling, below is the error I get when I try to run the program
Exception in thread "main" java.lang.NullPointerException
at Bank.create(Bank.java:20)
at BankDriver.main(BankDriver.java:7)
Re: Getting NullPointerException in program
Code :
if(accountNum != accounts[count].getAcctNumber())
Step through the code and consider the operations one by one. When the above line is first executed, has accounts[count] yet been instantiated? You should validate this in the code (you can do so simply by either checking for null or checking the index is > 0.)
Re: Getting NullPointerException in program
if(accountNum != accounts[count].getAcctNumber())
For your first time, the array will be created but every slot, including index 0, will be empty. That's causing your Null Pointer Exception I think.