0

I want to read all the keys from lmdb into a list, using bufferCursor, but I am getting index out of bound error.

String[] keys=new String[keyCount]; //keyCount gives no. of keys in lmdb tree
int count=0;
cursor.first(); //to move to first position
while(cursor.next){
    keys[count]= cursor.keyUtf8(0).getString().toString(); //getting error in this line it's working for reading values.
    count++l
}
mr.celo
  • 585
  • 1
  • 5
  • 17
xrs
  • 144
  • 11

1 Answers1

0

It seems like keyCount is to low of a number, or maybe even zero.

If you don't know how many key are going to be in an array, initialize the String[] array with a number larger than zero. Then, as you add more items, make sure keys.length() is large enough to accept a new item. If not, use Arrays.copyOf() to increase its size. Check out the Javadoc for the function here

Also, you can use a higher level array, like ArrayList. This will ensure you never add more items than the array can handle, by performing the copyOf() method internally automatically.

Such as:

ArrayList<String> keys = new ArrayList<String>(keyCount); //keyCount gives no. of keys in lmdb tree
int count=0;
cursor.first(); //to move to first position

while(cursor.next){
   keys.add(cursor.keyUtf8(0).getString().toString()); //getting error in this line it's working for reading values.
   count++l
}
mr.celo
  • 585
  • 1
  • 5
  • 17
  • It's an internal error on lmdb library on cursor, i am able to read values in same way correctly – xrs Nov 26 '15 at 01:16