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
Sambhav Anand
2,010 PointsI 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
Kristen Law
16,244 PointsThe 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.
Sambhav Anand
2,010 PointsSambhav Anand
2,010 Pointsthen how do initialize it so that i can use it inside the onCreate() method.
Kristen Law
16,244 PointsKristen Law
16,244 PointsWhy do you need to set the text of
mTextViewinside theonCreate()method? You can't initializemLastLocationin theonCreate()method because you need to wait for theonConnected()function callback to get called before you can get the location.Sambhav Anand
2,010 PointsSambhav Anand
2,010 PointsSo how do I handle the callback
Kristen Law
16,244 PointsKristen Law
16,244 PointsHave you tried what I suggested in my first comment?
Sambhav Anand
2,010 PointsSambhav Anand
2,010 PointsYa 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
Kristen Law
16,244 PointsKristen Law
16,244 PointsYou'll need to add
mGoogleApiClient.connect()somewhere, preferably in theonStart()method. From the Accessing Google APIs documentation: