Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

readRecord : Index out of bounds exception

 
Theo van Loon
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the client starts i read all records from the databasefile. I keep getting an Index out of bounds exception. I try to set out what's happening :
I have a method getRecords() that return a DataInfo[] containing records. In this method i execute the following :

In readRecord i execute the following :

I don't understand where it goes out of bound. Can somebody help me?!!
[Andrew: put code between [code] and [/code]tags]
[ March 08, 2004: Message edited by: Andrew Monkhouse ]
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check up the line number where the exception was thrown?
 
Theo van Loon
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if i can post this but this is the Exception Message
java.lang.IndexOutOfBoundsException
at java.io.RandomAccessFile.readBytes(Native Method)
at java.io.RandomAccessFile.read(RandomAccessFile.java:307)
at suncertify.db.DataHelper.readRecord(DataHelper.java:226)
at suncertify.db.DataHelper.getRecords(DataHelper.java:519)
at suncertify.db.Data.getRecords(Data.java:288)
at suncertify.db.LocalDataAccess.getRecords(LocalDataAccess.java:62)
at suncertify.client.ContractorTableModel.<init>(ContractorTableModel.java:33)
at suncertify.client.Client.init(Client.java:80)
at suncertify.client.Client.<init>(Client.java:59)
at suncertify.client.Client.main(Client.java:64)
Exception in thread "main"
In the readRecord it goes wrong in the for loop.
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a very quick glance, you should probably used

records[i-1] = new DataInfo(this.readRecord(i));

in stead of

records[i] = new DataInfo(this.readRecord(i));

[ March 08, 2004: Message edited by: Jacques Bosch ]
 
Theo van Loon
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately that's not it, i still get the same index out of bounds exception.
 
Theo van Loon
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where it all goes wrong is at the read method, but i don't seem to understand why.
The length of the field to be read is correct.
Based on this information i construct a byte[]
byte[] bytes = new byte[fields[i].getLength()];
This is still in order.
Then i read it :
raf.read(bytes, (int) raf.getFilePointer(), fields[i].getLength());
bytes is created, the current fileposition is correct and the fieldslength is correct. But still i get an index out of bounds exception...
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Theo,
There's no problem that I'm aware of posting stack traces. Feel free.
Regarding the code posted (please use UUB code tags so the indenting is preserverd):

Arrays in Java use indexes that start with zero. You should really try to use them in that way. The code posted above is misleading. If totalRecords is 10 the for loop is only going to execute 9 times. If there are really 10 records then one record is being ignored. If there are really nine records, then totalRecords is a very misleading name. The same argument can be made with totalFields.
This is easily fixed by using a more conventional style:
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Theo,
I have edited your original post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.
When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.
If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.
There is certainly no problem with posting stack traces, and it can be very useful to do so.
Usually you can read stack traces from the top down until you see a method that you recognise as one of your methods. In the stack trace you provided, the first two methods are from the Java API, so we can ignore them. The third line is one of your methods:

This then identifies exactly where you are referencing an out of bounds exception. It is in the readRecord method, at line 226 of the DataHelper.java file.
This is almost certainly the line:

The problem is with the second parameter.
Short answer - you probably don't need to use this particular method. You will probably find that read(byte[]) will give you the desired result. Or even better - readFully(byte[]).
Long answer - the offset in the second parameter is not the offset into the file, but the offset into the array. So you are exceeding the size of the array.
The confusing part is that the API reads (IMHO) as though it should be the offset into the file. But here is a simple program to test this:

And when I run it:

So you see what it is doing now?
Regards, Andrew
 
Theo van Loon
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Andrew. This has been very helpful. Indeed I was assuming that int off in raf.read was the postition in the RandomAccessFile from where to read.
Thanks a lot for your help!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic