Passing an Integer with an Intent results in a null Object reference

Salatgurke :

I am ceating a new intent in a method to pass an Integer value to another activity. However, I get a Null Object Reference / Null Pointer Exception on the receiving end. This is the calling method (Insect is a custom type with an ID that is an integer)

private void openInsectDetails(Insect insect){
        Intent intent = new Intent(InsectCatchable.this, InsectDetails.class);
        intent.putExtra("InsectDetails", insect.getId());
        InsectCatchable.this.startActivity(intent);
    }

This is the receiving class:

package com.ac_companion.wegner;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;

public class InsectDetails extends AppCompatActivity {
    private final String TAG = "InsectDetails";
    private int insectPositionMarker;
    private Insect localInsect;
    public int insectId;
    //private Intent mIntent = new Intent();

    public InsectDetails(){
        Intent mIntent = getIntent();
        insectId = mIntent.getIntExtra("InsectDetails", 0);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insect_details);
        localInsect = Util.getGlobalInsectList().get(insectId);
    }

}

This is the Exception:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ac_companion.wegner, PID: 16344
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ac_companion.wegner/com.ac_companion.wegner.InsectDetails}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
        at com.ac_companion.wegner.InsectDetails.<init>(InsectDetails.java:20)
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

Why is my mIntent.getIntExtra a null object? To my unterstanding, its properly initialized.

Stanislav Batura :

I think it's because of activity lifecycle. You shouldn't use a constructor in activity. Just move your code in @OnCreate methode

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_insect_details);

    Intent mIntent = getIntent();
    insectId = mIntent.getIntExtra("InsectDetails", 0);
    localInsect = Util.getGlobalInsectList().get(insectId);
}

You get an exception because you trying to get intent before activity is created.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403731&siteId=1