• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Undetectable error in code

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help resolve this error.
I have tried everything I knew but can't resolve this error.

1. Record Transaction
2. View Record
5. Exit

Select Your Operation: 2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at accounting.engine.ViewRecords.viewGeneralJournal(ViewRecords.java:24)
at accounting.engine.BackGround.viewingOfRecords(BackGround.java:48)
at accounting.engine.AccountingEngine.main(AccountingEngine.java:43)
Java Result: 1

BUILD SUCCESSFUL (total time: 3 seconds)
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> accounting.engine.ViewRecords.viewGeneralJournal(ViewRecords.java:24)

At that line of code you must be accessing an ArrayList like: list.get(0) -> which should get you the first value of the list. But:

> java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

This part of the exception tells you the ArrayList you are accessing has a Size of 0, so the Index of 0 (the first Object in the list) can't be found. So your problem is that you are trying to get something from an empty ArrayList. The solution is to avoid doing that. Check the size of the ArrayList before you retrieve anything from it. And if not (Size of List > Index To Retrieve) show an error saying the the desired record does not exist.
 
Ranch Hand
Posts: 66
Android IntelliJ IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IndexOutOfBounds indicates you are attempting to access a member that doesn't exist. Remember that array indices are zero-based.

Be sure your array (ArrayList?) actually contains items and that you are not trying to access an index outside the valid range.
 
Sadaqatullah Noonari
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:> accounting.engine.ViewRecords.viewGeneralJournal(ViewRecords.java:24)

At that line of code you must be accessing an ArrayList like: list.get(0) -> which should get you the first value of the list. But:

> java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

This part of the exception tells you the ArrayList you are accessing has a Size of 0, so the Index of 0 (the first Object in the list) can't be found. So your problem is that you are trying to get something from an empty ArrayList. The solution is to avoid doing that. Check the size of the ArrayList before you retrieve anything from it. And if not (Size of List > Index To Retrieve) show an error saying the the desired record does not exist.




I read that file from the hard disk which had 5 different elements and all those were saved in this ArrayList but when I print the ArrayList using the following code, it says above error.

for(int i =0; i<this.GeneralJournal.size();i++){
System.out.println(this.GeneralJournal.get(i));
}
>
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try printing the ArrayList like this:

What do you get?
 
Sadaqatullah Noonari
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Try printing the ArrayList like this:

What do you get?



following is the output after that statement


[Supplies, Drawings, Equipment, Supplies, Revenue]
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, that does seem strange. i bet it is not that hard though, once you figure it out.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sadaqatullah Noonari wrote:for(int i =0; i<this.GeneralJournal.size();i++){
System.out.println(this.GeneralJournal.get(i));
}


If that code is causing the exception then my guess is that another thread is removing the elements between the call to size() in the for-loop check and the call to get() in the printout.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and you might get better help if you post the ViewRecords source file (or at least the ViewRecords.viewGeneralJournal method)
 
reply
    Bookmark Topic Watch Topic
  • New Topic