Caused by: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference
Solution 1
The statement
mGoogleApiClient.connect();
should appear after mGoogleApiClient
has been instantiated
Solution 2
You are invoking the method
connect()
on the object
mGoogleApiClient
without instantiating it
You need to write this first,
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
then this,
mGoogleApiClient.connect();
Solution 3
You can't call connect()
until after you initialize the mGoogleApiClient
reference. Like,
// mGoogleApiClient.connect();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect(); // <-- move to here.
Solution 4
Try this process : Assign google client before loading contentview:
private GoogleApiClient mGoogleApiClient;
public static int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
setContentView(R.layout.activity_game_over);
}
Add override onStart and connect the google client.
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
l7ivine
Updated on September 12, 2020Comments
-
l7ivine over 3 years
I am having trouble submitting the highscores to android leaderboards. Right now the leaderboards are empty and nothing is being submitted. I have an int "oldScore" that needs to be submitted. Right now I am trying a piece of code to submit it but the activity crashes when called. My code:
public class GameOver extends BaseGameActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private GoogleApiClient mGoogleApiClient; public static int score; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game_over); mGoogleApiClient.connect(); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); int newScore = GameOver.score; SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); int oldScore = prefs.getInt("key", 0); if (newScore > oldScore) { SharedPreferences.Editor edit = prefs.edit(); edit.putInt("key", newScore); edit.commit(); EditText HighScore = (EditText) findViewById(R.id.HighScoreT); HighScore.setText("" + newScore); } else { EditText HighScore = (EditText) findViewById(R.id.HighScoreT); HighScore.setText("" + oldScore); Games.Leaderboards.submitScoreImmediate(getApiClient(),String.valueOf(R.string.number_guesses_leaderboard), oldScore); } }
Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' on a null object reference
-
l7ivine over 9 yearsWhen I do this I then get this error : "java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet."
-
l7ivine over 9 yearsWhen I do this I then get this error : "java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet."
-
l7ivine over 9 yearsWhen I do this I then get this error : "java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet."
-
Elliott Frisch over 9 yearsOkay. That sounds like we've fixed your
NullPointerException
. Have you done any research at all on your new question? -
Sanjeet A over 9 yearsYou are using the google login, You should call the method from onStart()
-
l7ivine over 9 yearsI just created methods onStart() and onStop() and put mGoogleApiClient.connect(); in OnStart yet i am getting the same error.
-
l7ivine over 9 yearswhen i delete "Games.Leaderboards.submitScoreImmediate(getApiClient(), String.valueOf(R.string.number_guesses_leaderboard), oldScore);" my activity works fine.. so this is where my error is coming from. How should i change this to submit the score?