0

I am trying to populate a spinner from a sqlite database. The database is +400mb and i manually dropped into /data/data/myapplicationpackage/databases directory in my sdcard.

I also have a ContentProvider to access the data. Here is some relevant part of the code:

private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;
private static final String DOSIS_PA_TABLE = "mytable";

public static final String KEY_ID_PA = "_id";
public static final String KEY_DESCRIPTION = "description";


public static final String CONTENT_AUTHORITY = "com.mypackage.myappname";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://"
        + CONTENT_AUTHORITY);

private static final String PATH_DOSISPED_PA = "mytable";

public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
        .appendPath(PATH_DOSISPED_PA).build();

public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.myappname.mytable";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.myappname.mytable";

private static final int DOSISPAS = 1;
private static final int DOSISPA_ID = 2;

private static final UriMatcher uriMatcher;
static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI("CONTENT_AUTHORITY", "mytable", DOSISPAS);
    uriMatcher.addURI("CONTENT_AUTHORITY", "mytable/*", DOSISPA_ID);
}

And this is part of the activity where i trigger the query:

contentResolver = getContentResolver();
    Uri uri = MyContentProvider.CONTENT_URI;
    Cursor cursor = contentResolver.query(uri, null, null, null, null);

    // create an array to specify which fields we want to display
    String[] from = new String[] { MyContentProvider.KEY_DESCRIPTION };
    // create an array of the display item we want to bind our data to
    int[] to = new int[] { android.R.id.text1 };
    // create simple cursor adapter
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_spinner_item, cursor, from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // get reference to our spinner
    Spinner spinner = (Spinner) findViewById(R.id.spinner_pas);
    spinner.setAdapter(adapter);//THIS IS WHERE THE EXCEPTION IS THRWON

And a NullPointerException is thrown at this line of code:

    spinner.setAdapter(adapter);

And here is the logcat:

10-02 18:41:32.109: E/AndroidRuntime(2783): FATAL EXCEPTION: main
10-02 18:41:32.109: E/AndroidRuntime(2783): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edoctores.android.apps.idoctus/com.edoctores.android.apps.idoctus.ui.PediatricDosisActivity}: java.lang.NullPointerException
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.os.Looper.loop(Looper.java:137)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at java.lang.reflect.Method.invokeNative(Native Method)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at java.lang.reflect.Method.invoke(Method.java:511)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at dalvik.system.NativeStart.main(Native Method)
10-02 18:41:32.109: E/AndroidRuntime(2783): Caused by: java.lang.NullPointerException
10-02 18:41:32.109: E/AndroidRuntime(2783):     at com.edoctores.android.apps.idoctus.ui.PediatricDosisActivity.onCreate(PediatricDosisActivity.java:72)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.Activity.performCreate(Activity.java:5008)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-02 18:41:32.109: E/AndroidRuntime(2783):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-02 18:41:32.109: E/AndroidRuntime(2783):     ... 11 more

I have been the whole day trying to fix it without any luck. Could someone help me and point me into the right direction? Thanks a lot in advance

3
  • 3
    Post the logcat showing the exception please. Commented Oct 2, 2012 at 16:32
  • No, the code is annotated with the error. Pindleskin. Please post the entire stacktrace for the exception. It will tell us if spinner is null or adapter is null. PS. A one gallon tub of Ben and Jerrys Phish Food ice cream says it's spinner. PPS. Actually, it must be spinner since adapter is referenced immediately before. Phew, the ice cream's safe.. Commented Oct 2, 2012 at 16:57
  • Hello Sam, that is the entire stacktrace from logcat Commented Oct 2, 2012 at 16:57

1 Answer 1

1

findViewById() only returns a View if it is currently shown otherwise it returns null. You must have a Spinner with the id spinner_pas in the layout that you passed to setContentView() (or in a layout that has otherwise already been inflated).

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

Comments

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.