Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

I am getting an error while trying to code a basic location app

I am getting the following error when I am trying to run my code:

Unable to start activity ComponentInfo{com.example.sambhav.location/com.example.sambhav.location.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.location.Location.toString()' on a null object reference

My main activity code is:

public class MainActivity extends ActionBarActivity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

    private Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.textView);
        buildGoogleApiClient();

        mTextView.setText(mLastLocation.toString());
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
}

1 Answer

The error you are getting is basically saying that you are trying to call toString() on a null Location object. This means that mLastLocation hasn't been initialized yet and you are trying to use it. Moving this line

mTextView.setText(mLastLocation.toString());

into onConnected() after it has been initialized may accomplish what you are looking for.

then how do initialize it so that i can use it inside the onCreate() method.

Why do you need to set the text of mTextView inside the onCreate() method? You can't initialize mLastLocation in the onCreate() method because you need to wait for the onConnected() function callback to get called before you can get the location.

So how do I handle the callback

Have you tried what I suggested in my first comment?

Ya I tried. The error has gone but my code is not running. How/When does the onConnected() method get called so that it can change the text of my TextView

You'll need to add mGoogleApiClient.connect() somewhere, preferably in the onStart() method. From the Accessing Google APIs documentation:

With the callback interfaces defined, you're ready to call connect(). To gracefully manage the lifecycle of the connection, you should call connect() during the activity's onStart() (unless you want to connect later), then call disconnect() during the onStop() method.