views:

74

answers:

1

Hi , i would like to show data from my database onto a spinner. how would that be possible? As there is an null error in my logcat.

Booking.java

public class Booking extends Activity 
{
    private DBAdapter db; 
    private Spinner colourSpinner;
    private String txtArrival;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        db.open();
        setContentView(R.layout.booking);
        Cursor c = db.getSpinnerData();
        startManagingCursor(c);
        String[] from = new String[]{txtArrival};
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        Spinner colourSpinner = (Spinner) findViewById( R.id.spinner );
        colourSpinner.setAdapter(adapter);
        }
}

DBAdapter.java

public Cursor getSpinnerData() throws SQLException 
        {
            Cursor mCursor =
                    db.query(DATABASE_TABLE_2, new String[] {  
                            KEY_ROWID2,
                            KEY_ARRIVAL,}, 
                            null,
                            null, 
                            null, 
                            null, 
                            null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

Logcat

08-02 08:39:27.123: ERROR/AndroidRuntime(746): Uncaught handler: thread main exiting due to uncaught exception
08-02 08:39:27.174: ERROR/AndroidRuntime(746): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.NullPointerException
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.os.Looper.loop(Looper.java:123)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at java.lang.reflect.Method.invoke(Method.java:521)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at dalvik.system.NativeStart.main(Native Method)
08-02 08:39:27.174: ERROR/AndroidRuntime(746): Caused by: java.lang.NullPointerException
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.database.sqlite.SQLiteCursor.getColumnIndex(SQLiteCursor.java:311)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:312)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:336)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:88)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at one.two.Booking.onCreate(Booking.java:27)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     ... 11 more

Thank you.

+2  A: 

You are getting a NullPointerException because you are supplying null to SimpleCursorAdapter.

String[] from = new String[]{txtArrival};

That needs to be something like:

String[] from = new String[]{KEY_ARRIVAL};

The purpose of the from parameter is to indicate what columns out of your Cursor should be mapped to the TextView widgets identified by the to parameter.

CommonsWare
Thanks for helping
User358218
There is still no data shown in the spinner?
User358218