l7ivine
l7ivine

Reputation: 81

Caused by: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference

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

Upvotes: 4

Views: 31051

Answers (4)

Sanjeet A
Sanjeet A

Reputation: 5227

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();

Upvotes: 9

Santhosh
Santhosh

Reputation: 89

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();
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201439

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.

Upvotes: 2

Reimeus
Reimeus

Reputation: 159754

The statement

mGoogleApiClient.connect();

should appear after mGoogleApiClient has been instantiated

Upvotes: 11

Related Questions