Attempt to invoke virtual method '...' on a null object reference

25,721

Check intent is null or not before you get data

Intent intent=this.getIntent();
if(intent !=null){
       // do something you want
    }

example

In ClassA:

Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);

In ClassB:

  Intent intent = getIntent();
     if(intent !=null){
          String[] myStrings = intent.getStringArrayExtra("strings");
      }
Share:
25,721
ahsen mughal
Author by

ahsen mughal

Updated on January 09, 2020

Comments

  • ahsen mughal
    ahsen mughal over 4 years

    I getting the "Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null " error in some devices:

    Stack Trace:

    Exception java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.facebook.misstest/com.facebook.misstest.LoveMeterResultActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
    android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3254)
    android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3350)
    android.app.ActivityThread.access$1100 (ActivityThread.java:222)
    android.app.ActivityThread$H.handleMessage (ActivityThread.java:1795)
    android.os.Handler.dispatchMessage (Handler.java:102)
    android.os.Looper.loop (Looper.java:158)
    android.app.ActivityThread.main (ActivityThread.java:7229)
    java.lang.reflect.Method.invoke (Method.java)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run       
    (ZygoteInit.java:1230)
    com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
    Caused by java.lang.NullPointerException: Attempt to invoke virtual method    'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
    com.facebook.misstest.LoveMeterResultActivity.onCreate      
    (LoveMeterResultActivity.java:133)
    android.app.Activity.performCreate (Activity.java:6876)
    android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1135)
    android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3207)
    android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3350)
    

    Here is the code which is send data to the next activity:

                //send this result to LoveMeterResultActivity
                final Bundle basket = new Bundle();
                basket.putStringArray("key", loveData);
                        Intent intent = new Intent(LoveMeterActivity.this, LoveMeterResultActivity.class);
                        intent.putExtras(basket);
                        startActivity(intent);
    
  • ahsen mughal
    ahsen mughal over 7 years
    i think it can be work, but i'm going to test in different devices before accept the awnser
  • Charuක
    Charuක over 7 years
    @ahsen mughal as your wish :))