5 Replies - 372 Views - Last Post: 19 December 2013 - 02:43 AM Rate Topic: -----

#1 A.shizu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-November 13

Array of objects. Nullpointerexception.

Posted 17 December 2013 - 04:32 PM

Hello. I'm trying to design a program for a bank that manages up to 30 savings accounts. Each customer should be able to make deposits and withdrawals. Also, there should be a method to add 3% interest to all accounts whenever it is invoked.
I've tried various ways of doing this program, and I feel like I have no idea what I am doing anymore, or if what I have so far is even right.

The account information is printing, but I'm getting a NullPointerException.

import java.text.NumberFormat;

public class BankArray
{
   public static void main (String[] args)	 
	{	
	Bank LLBank = new Bank();   
		
	LLBank.print();          //This is one of the lines where I get NullPointerException
	}
}		
		
class Bank 
{
    private Account[] LLBank;				
    private int index = 0;
	
     public Bank() 	{
     LLBank = new Account[30];
     LLBank[0] = new Account ("Ted Murphy", 72354, 102.56);
     LLBank[1] = new Account ("Jane Smith", 69713, 40.00);
     LLBank[2] = new Account ("Edward Demsey", 93757, 759.32);
     LLBank[0].deposit (25.85);
 }
 public void print()  {
     System.out.println();
     for (int index=0; index < 30; index++)
       LLBank[index].print();		//also getting NullPointerException here
   }
}                          

class Account
{
	private final double RATE = 0.03;     // interest rate of 3%
	
	private long acctNumber;
	private double balance;
	private String name;
	
	// Sets up account by defining owner, account number, and initial balance
	public Account (String owner, long account, double initial)
	{
		name = owner;
		acctNumber = account;
		balance = initial;
	}
	
	// Deposits specified amount into account. Returns new balance.
	public double deposit (double amount)
	{
		balance = balance + amount;
		return balance;
	}
	
	// Withdraws specified amount from account and pplies fee. Returns the new balance.
	public double withdraw (double amount, double fee)
	{	
		balance = balance - amount - fee;
		
		return balance;
	}
	
	// Adds interest to account and returns new balance
	public double addInterest ()
	{ 
		balance += (balance * RATE);
		return balance;
	}
	
	// Returns current balance of account.
	public double getBalance ()
	{
		return balance;
	}
	public void print () {
		NumberFormat fmt = NumberFormat.getCurrencyInstance();
     System.out.println (acctNumber + "\t" + name + "\t" + fmt.format(balance));
   }

}


Is This A Good Question/Topic? 0
  • +

Replies To: Array of objects. Nullpointerexception.

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Array of objects. Nullpointerexception.

Posted 17 December 2013 - 04:44 PM

http://technojeeves....free/118-errors



Quote

Errors PDF Print E-mail
Written by Charles
Monday, 12 December 2011 17:22
If you don't tell us the exact error messages you get, we can't tell you what's wrong, and it's also useful to know exactly what command you're typing where appropriate.

If it's a Java programming error:

a. is it a compile time error? Paste the exact compiler error
b. is it a runtime error? Paste the full exception stack trace if there's an exception. For Exception 'e', that would be obtained with e.printStackTrace();

This post has been edited by jon.kiparsky: 17 December 2013 - 04:53 PM
Reason for edit:: Included text from link for the convenience of others

Was This Post Helpful? 0
  • +
  • -

#3 A.shizu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-November 13

Re: Array of objects. Nullpointerexception.

Posted 17 December 2013 - 04:54 PM

Exception in thread "main" java.lang.NullPointerException
at Bank.print(BankArray2.java:28)
at BankArray2.main(BankArray2.java:9)
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Array of objects. Nullpointerexception.

Posted 17 December 2013 - 05:06 PM

Quote

for (int index=0; index < 30; index++)
Any index above 2 has no (a null) instance

This post has been edited by g00se: 17 December 2013 - 05:07 PM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#5 A.shizu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-November 13

Re: Array of objects. Nullpointerexception.

Posted 18 December 2013 - 11:00 PM

View Postg00se, on 17 December 2013 - 07:06 PM, said:

Quote

for (int index=0; index < 30; index++)
Any index above 2 has no (a null) instance


I apologize for replying so late. Thank you. I can't believe I missed that.
So would the following code be a good solution? (I already tried it and it works)
     for (int index=0; index < LLBank.length; index++) {
      	if (LLBank[index]!=null) {
	    LLBank[index].print();
      	    }		
   	}


Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Array of objects. Nullpointerexception.

Posted 19 December 2013 - 02:43 AM

That will work but is not particularly efficient - if you're using the array as i suspect, then you're going to waste iterations just reading nulls. You need to stop the loop. Have another go
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1