0

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?

3
  • 1
    Possible duplicate of What is a NullReferenceException and how do I fix it? Commented Jan 20, 2016 at 20:16
  • The error message is telling you that the int is not being sent. Instead a null object is being picked up. Commented Jan 20, 2016 at 20:39
  • @MsYvette The possible duplicate has an excellent answer with 1000+ votes, which is valid for both Java and C#. The language and syntax might be different but the reason for exception and ways to debug it are very similar. Commented Jan 21, 2016 at 4:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.