IllegalArgumentEx: column '_id' does not exist - but it does

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

IllegalArgumentEx: column '_id' does not exist - but it does

Postby emooney » Fri Jan 29, 2010 2:41 am

I've seen other people having problems with the '_id' column throwing an IllegalArgumentException but none of the solutions have worked for me. I also believe the problem is happening somewhere in the 'SimpleCursorAdapter' object because in debug mode, this is where I'm breaking.

Also, The only time I'm seeing this problem is when I use the 'GROUP BY' field when creating the Cursor object. If I make that field a 'null', the list returns without an error, but it doesn't display correctly. I think it's weird that I'm only seeing this when I use "GROUP BY".

Here are my static variables:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         // Exercise Table fields
  3.  
  4.         public static final String KEY_EX_ROWID = "_id";
  5.  
  6.         public static final String KEY_EX_NAME = "name";
  7.  
  8.  
  9.  
  10.         // Session Table fields
  11.  
  12.         public static final String KEY_SESS_ROWID = "_id";
  13.  
  14.         public static final String KEY_SESS_EX_ROWID = "sess_ex_id";
  15.  
  16.         public static final String KEY_SESS_DATE = "date";
  17.  
  18.  
  19.  
  20.         private static final String DATABASE_NAME = "workoutasst";
  21.  
  22.         private static final String EXERCISE_TABLE = "exercises";
  23.  
  24.         private static final String SESSION_TABLE = "session";
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



Here's my call to the db to get my list:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         // code translates to this:
  3.  
  4.         // SELECT exercises._id, name, date FROM exercises LEFT JOIN session ON exercises._id = session.sess_ex_id GROUP BY name ORDER BY name,date DESC
  5.  
  6.         public Cursor fetchAllExercises() {
  7.  
  8.                 return mDb.query(EXERCISE_TABLE +
  9.  
  10.                                         " LEFT JOIN " +
  11.  
  12.                                         SESSION_TABLE
  13.  
  14.                                         + " ON " +
  15.  
  16.                                         EXERCISE_TABLE + "." + KEY_EX_ROWID + " = " +
  17.  
  18.                                         SESSION_TABLE + "." + KEY_SESS_EX_ROWID,
  19.  
  20.                                 new String[] {
  21.  
  22.                                         EXERCISE_TABLE + "." + KEY_EX_ROWID,
  23.  
  24.                                         KEY_EX_NAME,
  25.  
  26.                                         KEY_SESS_DATE },
  27.  
  28.                                 null,
  29.  
  30.                                 null,
  31.  
  32.                                 KEY_EX_NAME, // <-- when 'KEY_EX_NAME' is put in for the 'GROUP BY' field, I break;
  33.  
  34.                                 null,
  35.  
  36.                                 KEY_EX_NAME + "," +
  37.  
  38.                                 KEY_SESS_DATE + " DESC");
  39.  
  40.         }
  41.  
  42.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Here's the error:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Uncaught handler: thread main exiting due to uncaught exception
  3.  
  4.  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emooney.workoutTest/com.emooney.workoutTest.Workout}: java.lang.IllegalArgumentException: column '_id' does not exist
  5.  
  6.      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
  7.  
  8.      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
  9.  
  10.      at android.app.ActivityThread.access$1800(ActivityThread.java:112)
  11.  
  12.      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
  13.  
  14.      at android.os.Handler.dispatchMessage(Handler.java:99)
  15.  
  16.      at android.os.Looper.loop(Looper.java:123)
  17.  
  18.      at android.app.ActivityThread.main(ActivityThread.java:3948)
  19.  
  20.      at java.lang.reflect.Method.invokeNative(Native Method)
  21.  
  22.      at java.lang.reflect.Method.invoke(Method.java:521)
  23.  
  24.      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
  25.  
  26.      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
  27.  
  28.      at dalvik.system.NativeStart.main(Native Method)
  29.  
  30.  Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
  31.  
  32.      at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
  33.  
  34.      at android.widget.CursorAdapter.init(CursorAdapter.java:111)
  35.  
  36.      at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
  37.  
  38.      at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
  39.  
  40.      at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:85)
  41.  
  42.      at com.emooney.workoutTest.Workout.fillData(Workout.java:38)
  43.  
  44.      at com.emooney.workoutTest.Workout.onCreate(Workout.java:21)
  45.  
  46.      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
  47.  
  48.      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
  49.  
  50.      ... 11 more
  51.  
  52.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Someone please help. This is wreaking havoc on my obsessive compulsive disorder! :?
All files are attached.

main.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6.  xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.  android:orientation="vertical"
  9.  
  10.  android:layout_width="wrap_content"
  11.  
  12.  android:layout_height="wrap_content">
  13.  
  14.     <TextView
  15.  
  16.        android:layout_width="wrap_content"
  17.  
  18.        android:layout_height="wrap_content"
  19.  
  20.        android:text="Workout Assistant"/>
  21.  
  22.        
  23.  
  24.     <ListView
  25.  
  26.         android:id="@+id/android:list"
  27.  
  28.        android:layout_width="wrap_content"
  29.  
  30.        android:layout_height="wrap_content"
  31.  
  32.        
  33.  
  34.        />
  35.  
  36.         <TextView
  37.  
  38.                 android:id="@+id/android:_id"
  39.  
  40.        android:layout_width="wrap_content"
  41.  
  42.        android:layout_height="wrap_content"/>
  43.  
  44.                  
  45.  
  46.         <TextView
  47.  
  48.                 android:id="@+id/android:empty"
  49.  
  50.        android:layout_width="wrap_content"
  51.  
  52.        android:layout_height="wrap_content"
  53.  
  54.        android:text="No Exercises!"/>  
  55.  
  56.  
  57.  
  58. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Here's exercise_row.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6.  xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.  android:orientation="vertical"
  9.  
  10.  android:layout_width="wrap_content"
  11.  
  12.  android:layout_height="wrap_content">
  13.  
  14.  
  15.  
  16.         <TextView android:id="@+id/exercise"
  17.  
  18.             android:layout_width="wrap_content"
  19.  
  20.             android:layout_height="wrap_content"
  21.  
  22.             android:textSize="36dip"
  23.  
  24.            
  25.  
  26.             />
  27.  
  28.            
  29.  
  30.         <TextView android:id="@+id/date"
  31.  
  32.             android:layout_width="wrap_content"
  33.  
  34.             android:layout_height="wrap_content"
  35.  
  36.             android:textSize="14dip"
  37.  
  38.             />    
  39.  
  40. </LinearLayout>
  41.  
  42.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Attachments
WorkoutDbAdapter.java
(3.07 KiB) Downloaded 132 times
Workout.java
(1.42 KiB) Downloaded 130 times
emooney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jan 26, 2010 11:03 pm

Top

Postby jwei512 » Fri Jan 29, 2010 12:45 pm

You should be careful with how SQL formats JOINS like the one you are using. Specifically, for a join like this, the columns returned might be:

Exercise._id, name, Session._id, sess_ex_id, date

I just took a database class and typically when you natural join on two tables with the same column name SQL differentiates the two by appending the table name in front.

If this is the case, what you need to do is an "AS" statement somewhere, i.e. add "Exercise._id AS _id" and that should fix it.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby emooney » Fri Jan 29, 2010 6:10 pm

wow! That did it!
Can I buy you lunch? Dinner? one of your books or something?
I'm very grateful.

Thank you.
Eric

ps. I've subscribed to your rss feed. Looks very useful.
emooney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jan 26, 2010 11:03 pm

Postby jwei512 » Fri Jan 29, 2010 10:24 pm

Online lunch? Haha that might be a little bit tricky =P
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests