• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

ArrayIndexOutOfBoundsException

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm trying to program a global string keeping a history of all transactions made.
For that I created an Array but when I try to assign it a String I get an OutOfBound error.
It seems to be because of the 2nd dimension being first a 0.
But this int supposedly gets bigger when the methods are run.



Any tips on what I could change ?
Here is the exact error I get when compiling:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
       at BankAccount.deposit(BankAccount.java:19)
       at BankAccount.main(BankAccount.java:56)

Thank you !
 
Bartender
Posts: 2266
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you elaborate little more on your exact requirement?
 
Ranch Hand
Posts: 33
Android Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private static String[][] transactions=new String[6][number];

Here is the mistake. At this point number has the value 0. So the size of your array is always [6][0] because the size of an array is defined at the time of object creation and can't be changed later. If you want an array with variable size you should use ArrayList.
 
Marshal
Posts: 71022
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ben Ernstberger wrote:. . . Here is the mistake.  . . .

Good catch.

What do you think of the notion of having such a static field in the first place?
 
Ben Ernstberger
Ranch Hand
Posts: 33
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think that's a good idea. Instead, I would write a class bank that stores all accounts and their transactions.
 
Campbell Ritchie
Marshal
Posts: 71022
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ben Ernstberger wrote:I do not think that's a good idea. . . .

Nor did I. The bank class is a good suggestion.
 
Emma Vande-Wouwer
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the suggestions. The ArrayList worked fine for what I needed in my program. Seemed easier at my level.
I actually have no idea how to do a class which stores the transaction number for an undefined number of accounts.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic