what is wrong with my code? help me debug please.

Put your problem here if it does not fit any of the other categories.

what is wrong with my code? help me debug please.

Postby moonraker928 » Sat Jan 19, 2008 12:53 am

Pasted code below.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static final String[] CONTENT_PROJECTION = new String[] {
  2.  
  3.             PhotoContent.Photos._ID, PhotoContent.Photos.CREATED_DATE };
  4.  
  5.  
  6.  
  7. // get current image IDs
  8.  
  9. Cursor current_images = managedQuery(getIntent().getData(), CONTENT_PROJECTION, null, null);
  10.  
  11.        
  12.  
  13. // store current image ids in another array
  14.  
  15. String[] cur_image_id = new String[current_images.count()];
  16.  
  17. int stored_image_id = current_images.getColumnIndex(PhotoContent.Photos._ID);
  18.  
  19. for (int i = 0; i < current_images.count(); i++, current_images.next()) {
  20.  
  21.         cur_image_id[i] = current_images.getString(stored_image_id);
  22.  
  23. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I am getting a RuntimeException invoked by the Looper.class...and have done some debugging to localize the problem to the snippet of code posted above.

What am I doing wrong?
moonraker928
Freshman
Freshman
 
Posts: 9
Joined: Wed Jan 16, 2008 11:34 pm

Top

Postby plusminus » Sat Jan 19, 2008 2:38 pm

Hello moonraker,

on which line exactly is it thrown :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby moonraker928 » Sat Jan 19, 2008 11:21 pm

the error is thrown inside the for loop.

the exact error goes like this:

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 8

im thinking it has something to do with current_images.next() as that goes through the rows of the cursor? but still, i have no idea why that would go out of bounds...

any ideas?
moonraker928
Freshman
Freshman
 
Posts: 9
Joined: Wed Jan 16, 2008 11:34 pm

Maybe you need to call cursor.first()?

Postby wataru » Sun Jan 20, 2008 11:50 am

Hi,

I could be wrong, but maybe try using

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(current_images.first())  // I believe this will move cursor to first row
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


before getting into your cursor iteration (=for) loop.
As a bonus, this should also take care
the case where there is no record returned as well :)

Hope it helps
wataru
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Dec 04, 2007 7:56 pm

Postby plusminus » Sun Jan 20, 2008 1:18 pm

Hey moonraker928,

have a look at the :src: Working with the SQLite-Database - Cursors-Tutorial.

This is how I did it there and it worked well:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                if (c != null) {
  2.                     /* Check if at least one Result was returned. */
  3.                     if (c.first()) {
  4.                          int i = 0;
  5.                          /* Loop through all Results */
  6.                          do {
  7.                               i++;
  8.                               // retrieve columns, etc...
  9.  
  10.                          } while (c.next());
  11.                     }
  12.                }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 11 guests