1

Since , I am new in android world so , please help me. When i am working with Android SQLite for retrieving data from data-base by these codes-

Cursor cursor;
DatabaseConnector dbConnector = new DatabaseConnector(this);
private ListAdapter conAdapter;
String[] from_for_text_in_list = {"name"};
int[] to_for_text_in_list = {R.id.title_info_txt_v}
cursor = dbConnector.getAllContacts();
conAdapter = new SimpleCursorAdapter(Infoclass.this, R.layout.listview_layout, null, from_for_text_in_list, to_for_text_in_list);
setListAdapter(conAdapter);

Now i declare getAllmethod() as-

public Cursor getAllContacts()
{
    return database.rawQuery("SELECT _id, name FROM tcc_info_table ", new String[]{"_id", "name"});
}

"name" is just a column name of database in my App.

While I am running my App it shows NULL POINTER Exception in getAllContact() method . Even i check my database through SQLite Browser and i found that data insertion going on successfully . But data not retrieve from database to list view . My logCat error is as below-

            11-14 12:01:21.694: E/Trace(30607): error opening trace file: No such file or directory (2)

11-14 12:01:22.475: D/AndroidRuntime(30607): Shutting down VM
11-14 12:01:22.475: W/dalvikvm(30607): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-14 12:01:22.495: E/AndroidRuntime(30607): FATAL EXCEPTION: main
11-14 12:01:22.495: E/AndroidRuntime(30607): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kt.narendramodi/com.kt.narendramodi.ModiForIndia}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kt.narendramodi/com.kt.narendramodi.InfoNarendraModi}: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range.  The statement has 0 parameters.
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.os.Looper.loop(Looper.java:137)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at java.lang.reflect.Method.invokeNative(Native Method)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at java.lang.reflect.Method.invoke(Method.java:511)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at dalvik.system.NativeStart.main(Native Method)
11-14 12:01:22.495: E/AndroidRuntime(30607): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kt.narendramodi/com.kt.narendramodi.InfoNarendraModi}: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range.  The statement has 0 parameters.
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1900)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.widget.TabHost.setCurrentTab(TabHost.java:358)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.widget.TabHost.addTab(TabHost.java:236)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at com.kt.narendramodi.ModiForIndia.onCreate(ModiForIndia.java:41)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.Activity.performCreate(Activity.java:5008)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-14 12:01:22.495: E/AndroidRuntime(30607):    ... 11 more
11-14 12:01:22.495: E/AndroidRuntime(30607): Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range.  The statement has 0 parameters.
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at com.kt.nanrendramodi.databasehelper.DatabaseConnector.getAllContacts(DatabaseConnector.java:60)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at com.kt.narendramodi.InfoNarendraModi.onCreate(InfoNarendraModi.java:94)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.Activity.performCreate(Activity.java:5008)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-14 12:01:22.495: E/AndroidRuntime(30607):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-14 12:01:22.495: E/AndroidRuntime(30607):    ... 21 more
15
  • Have you initialized your database variable? Commented Nov 14, 2012 at 10:05
  • I did not get you ... Will you please elaborate .? Commented Nov 14, 2012 at 10:20
  • 1
    You need to initialize your database object as database = dbHelper.getReadableDatabase() Commented Nov 14, 2012 at 10:22
  • database is object of SQLiteDataBase Class . Commented Nov 14, 2012 at 10:25
  • Ok. So you will need to initialize it like we initialize other objects. E.g. Student stu = new Student(); Commented Nov 14, 2012 at 10:27

1 Answer 1

3

You'll have to check 2 things:

  1. Your database should be initialized to a read/write-able database.

    database = yourDbHelper.getReadableDatabase();

  2. You don't need those selection arguments if you've filled the SQL query fully.

    database.rawQuery("SELECT _id, name FROM tcc_info_table ", null);

Sign up to request clarification or add additional context in comments.

2 Comments

Dear varevarao "database" is object of SqliteDataBase class .
I don't see how/why that means you shouldn't initialize it as described above. getReadableDatabase() returns an instance of an SQLiteDatabase. Refer to the link below, especially the line Create and/or open a database. developer.android.com/reference/android/database/sqlite/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.