views:

76

answers:

4

My Logcat

08-06 01:27:34.874: ERROR/AndroidRuntime(721): Uncaught handler: thread main exiting due to uncaught exception
08-06 01:27:34.893: ERROR/AndroidRuntime(721): java.lang.NullPointerException
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at one.two.Booking$MyOnItemSelectedListener.onItemSelected(Booking.java:53)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:856)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.widget.AdapterView.access$200(AdapterView.java:41)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:827)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.os.Handler.handleCallback(Handler.java:587)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.os.Looper.loop(Looper.java:123)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at java.lang.reflect.Method.invoke(Method.java:521)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at dalvik.system.NativeStart.main(Native Method)

Booking.java

package one.two;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;


public class Booking extends Activity
{
    private DBAdapter db; 
    private Spinner colourSpinner;
    public Cursor c;
    public TextView txtArrival;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        this.setContentView(R.layout.booking);
        db.open();
        fillData();
        db.close();
        TextView txtArrival = (TextView) findViewById(R.id.txtArrival);
        Spinner colourSpinner = (Spinner) findViewById(R.id.myspinner);
        colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }

    private void fillData()
    {
        c = db.getSpinnerData();
        startManagingCursor(c);
        String[] from = new String[]{DBAdapter.KEY_ARRIVAL};
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this,R.layout.booking, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        Spinner colourSpinner = (Spinner) findViewById(R.id.myspinner);
        colourSpinner.setAdapter(adapter);
    }
    public class MyOnItemSelectedListener implements OnItemSelectedListener
    {
        public void onItemSelected(AdapterView<?> arg0, View v,int position, long id)
        {
            boolean result = Booking.this.c.moveToPosition(position);
            if (result) {
                String title=Booking.this.c.getString(1);
                Booking.this.txtArrival.setText(title);
            }

        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }
    };
}

I would like to know how to solve this error. Thank you.

A: 

It looks like the error is on line 53, not 54.

In any case, you're dereferencing a null pointer. Just check and see which variable isn't defined, and avoid calling it (or make sure it's properly initialized).

Assuming you've listed the correct line, it's either txtArrival or Booking.this (I'm not sure if you can you call this on a class.).

Trevor Johns
Sorry, what do you mean by dereferencing a null pointer and how would i go about doing it?
User358218
You call Booking.this from an inner class of Booking to refer to the Booking object instead of the inner one.
Carl Manaster
So instead of Booking.this, i would change it to?
User358218
Every variable in Java is a reference to an object (or a primitive, but I digress). If you call myObj.foo(), Java first looks up what object myObj points to. If you say "myObj=null" first, then calling myObj.foo() is guaranteed to fail. That's what dereferencing a null pointer is.
Trevor Johns
But i did not declare any myobj = null in my codes?
User358218
A: 

I believe you need to make sure that the variable "C" is not null.

String title=c.getString(1); txtArrival.setText(title);

seems to be the trouble code, and if c is null, then c.getString(1) should return nullpointerexception, or it might just return nothing, causing title to be null, and setting txtarrival.setText with a null should throw an error too.

Nicholas
So would i go about solving it? As i already have c = db.getSpinnerData(), which has data in the cursor.
User358218
Add an if there that checks if either c is null, or title.
Nicholas
A: 

There is only one place on line 53 where you can get NPR: txtArrival.setText().

I believe you forgot to set TextView's ID to R.id.txtArrival in layout and (TextView) findViewById(R.id.txtArrival); returns NULL.

nunthrey
I already did TextView txtArrival = (TextView) findViewById(R.id.txtArrival); in my oncreate method.
User358218
+2  A: 

You are creating local variables with the same names as your class members. This leads to the members being hidden by the local ones which is why you are getting NullPointerExceptions. You are simply not giving the members any value which is why they are null.

What you want to do is:

package one.two;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;


public class Booking extends Activity
{
    private DBAdapter db; 
    private Spinner colourSpinner;
    public Cursor c;
    public TextView txtArrival;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        this.setContentView(R.layout.booking);
        txtArrival = (TextView) findViewById(R.id.txtArrival); // Member assignment
        colourSpinner = (Spinner) findViewById(R.id.myspinner); // Member assignment
        db.open();
        fillData();
        db.close();
        colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }

    private void fillData()
    {
        c = db.getSpinnerData();
        startManagingCursor(c);
        String[] from = new String[]{DBAdapter.KEY_ARRIVAL};
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this,R.layout.booking, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        // Removed this line, since members is set in onCreate
        colourSpinner.setAdapter(adapter);
    }
    public class MyOnItemSelectedListener implements OnItemSelectedListener
    {
        public void onItemSelected(AdapterView<?> arg0, View v,int position, long id)
        {
            boolean result = Booking.this.c.moveToPosition(position);
            if (result) {
                String title=Booking.this.c.getString(1);
                Booking.this.txtArrival.setText(title);
            }

        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }
    };
}
Key
There is still a null error stated, in my logcat
User358218
The error is at Booking.this.txtArrival.setText(title);
User358218
Have you used the code in my version of onCreate? I have moved line 7 and 8 of onCreate and removed the local variable creation. Make sure you understand why you had the null pointer from the beginning and you will be able to solve these issues much faster!
Key