This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Undetectable error in code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Undetectable error in code" Watch "Undetectable error in code" New topic
Author

Undetectable error in code

Sadaqatullah Noonari
Greenhorn

Joined: Mar 22, 2011
Posts: 13
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

Joined: Jan 28, 2003
Posts: 4165
    
  21

> 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.


Steve
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

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.


Keith Rainey
OCPJP6
Sadaqatullah Noonari
Greenhorn

Joined: Mar 22, 2011
Posts: 13
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

Try printing the ArrayList like this:

What do you get?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sadaqatullah Noonari
Greenhorn

Joined: Mar 22, 2011
Posts: 13
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

Joined: Oct 21, 2000
Posts: 4340
    
    2

well, that does seem strange. i bet it is not that hard though, once you figure it out.


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

and you might get better help if you post the ViewRecords source file (or at least the ViewRecords.viewGeneralJournal method)


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Undetectable error in code
 
Similar Threads
Checking arrays
addElement in XML
Issue with console
How to extract columns of coordinate values from text file (in java) ?
Sequencing Records