0

I have three activities. I send an object from one activity to second activity. I retrieve the data from second activity to first using onActivityResult. Then I need to send the data that I retrieved to the third activity but I have an exception.

Main Activity (question is a class that implement parcelable. You can see in other question First question:

public class MainActivity extends Activity {

Tgestion Tges= new Tgestion();
Button buttonI,buttonM,buttonB,buttonD,buttonS;
static final int RECUPERAR_GESTOR = 1;
static final int RECUPERAR_MODIFICAR=1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();

}

public void addListenerOnButton() {

    final Context context = this;

    buttonI = (Button) findViewById(R.id.buttonIntroducir);
    buttonI.setEnabled(true);

    buttonI.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, IntroducirPatron.class);
            intent.putExtra("com.example.sistemacontrasena.gestion", Tges);
            //startActivity(intent); 
            startActivityForResult(intent, RECUPERAR_GESTOR);

        }

    });

    buttonM = (Button) findViewById(R.id.buttonModificar);
    buttonM.setEnabled(false);

    buttonM.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


            Intent intent2 = new Intent(context, ModificarPatron.class);
            intent2.putExtra("com.example.sistemacontrasena.mod", Tges);
            startActivityForResult(intent2, RECUPERAR_MODIFICAR);   

        }

    });


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == RECUPERAR_GESTOR) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            this.Tges=data.getParcelableExtra("com.example.sistemacontrasena.result_gestion");


        }
        if (resultCode == RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }               
}
}

In the Second Activity I put the setResult:

Intent returnIntent = new Intent();
    returnIntent.putExtra("com.example.sistemacontrasena.result_gestion", this.gestion);
    setResult(RESULT_OK, returnIntent);
    return maximo;

Until here everything is correct In third Activity I use:

Tgestion gestion2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modificar_patron);
      this.gestion2=getIntent().getParcelableExtra("com.example.sistemacontrasena.mod");

}

but when I call a function of question2 in the third activity, for example:

 String[] prueba= new String[2];
 prueba=this.gestion2.getSecret();
 System.out.println(prueba[0])

I have an exception:

06-15 13:37:44.852: E/AndroidRuntime(32262): FATAL EXCEPTION: main
06-15 13:37:44.852: E/AndroidRuntime(32262): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sistemacontrasena/com.example.sistemacontrasena.ModificarPatron}: java.lang.NullPointerException
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.os.Looper.loop(Looper.java:138)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.app.ActivityThread.main(ActivityThread.java:3701)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at java.lang.reflect.Method.invoke(Method.java:507)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at dalvik.system.NativeStart.main(Native Method)
06-15 13:37:44.852: E/AndroidRuntime(32262): Caused by: java.lang.NullPointerException
06-15 13:37:44.852: E/AndroidRuntime(32262):    at com.example.sistemacontrasena.ModificarPatron.onCreate(ModificarPatron.java:35)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 13:37:44.852: E/AndroidRuntime(32262):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
06-15 13:37:44.852: E/AndroidRuntime(32262):    ... 11 more

I need to use the information retrieve from the second activity to the third activity through main activity.

Community
  • 1
  • 1
sandrita
  • 363
  • 1
  • 6
  • 17

1 Answers1

0

You are getting nullpointerexception on gestion2 reference in your 3rd activity. The key used in getParcelableExtra("com.example.sistemacontrasena.mod") doesn't contain any value since you haven't assigned any values to this key yet.

Just make sure you the keys you are using to retrieve values from already has some values assigned in the calling activity intent.

Put proper logs to check for null values, if any reference/object on which you are calling any method is null.

epiphany27
  • 517
  • 1
  • 10
  • 23