Simbilli Dev
Simbilli Dev

Reputation: 237

Getting "null object reference" error while trying to send an int with Intent

Code on sender Activity

Intent intent = new Intent(this, ScrollingActivity.class);
    intent.putExtra("KEY", 1);
    startActivity(intent);

Code on receiver Activity

int a = getIntent().getExtras().getInt("KEY");

While trying this getting this error log

java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference

Can you help for fix it?

Upvotes: 0

Views: 2049

Answers (1)

marcinj
marcinj

Reputation: 49976

Your error indicates that getIntent returns null reference, you should check where you are calling getIntent - it should be called inside or after Actvity.onCreate in Activity life cycle.

Upvotes: 2

Related Questions