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?

Simbilli Dev
  • 237
  • 1
  • 2
  • 8
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dotnetom Jan 20 '16 at 20:16
  • The error message is telling you that the `int` is not being sent. Instead a `null` object is being picked up. – Nathan Jan 20 '16 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. – dotnetom Jan 21 '16 at 04:41

1 Answers1

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.

marcinj
  • 48,511
  • 9
  • 79
  • 100