Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Undetectable error in code

 
Sadaqatullah Noonari
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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)
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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));
}
>
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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]
 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, that does seem strange. i bet it is not that hard though, once you figure it out.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic