Home > Enterprise >  java.lang.NullPointerException when trying to get a Long variable from an Intent
java.lang.NullPointerException when trying to get a Long variable from an Intent

Time:09-01

I will be very brief, I am making an attendance control application for teachers.

In the main activity the list of groups is loaded and each one has a button to edit its information. (I use recyclerview and adapters)

This happens when you click the Edit button:

In GroupAdapter Class>OnBindViewHolder

int lastPosition = holder.getAdapterPosition();
    long idGroupUP = mGroups.get(lastPosition).getId_group();
    holder.ibtnEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clicsListener.editClic(lastPosition, idGroupUP);
        }
    });

In MainActivity Class

@Override
public void editClic(int position, long idGroup) {
    Intent intent = new Intent(MainActivity.this, EditGroup.class);
    intent.putExtra("idGroup", idGroup);
    intent.putExtra("position", position);
    resultEditGroup.launch(intent);
}

In EditGroup Class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_group);
    editGroupViewModel = new ViewModelProvider(this).get(EditGroupViewModel.class);
    idGroup = getIntent().getExtras().getLong("idGroup");
    .
    .
    .
}

Once the group id is obtained, all the information is loaded from the database. So in the Crashes section of the Play Console I can see that 7 users have been affected by the following issue, This is what I read in PlayConsole:

    Exception java.lang.RuntimeException:
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3477)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3620)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:83)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2183)
  at android.os.Handler.dispatchMessage (Handler.java:107)
  at android.os.Looper.loop (Looper.java:241)
  at android.app.ActivityThread.main (ActivityThread.java:7617)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:492)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:941)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'long android.os.Bundle.getLong(java.lang.String)' on a null object reference
  at com.lista.asistencia.EditGroup.onCreate (EditGroup.java:86)
  at android.app.Activity.performCreate (Activity.java:7817)
  at android.app.Activity.performCreate (Activity.java:7806)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1328)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3452)

I don't see why this would happen, since the EditGroup.class is only initialized from that one button. I also did not see the need to put the rest of the code of my Adapter because it does not make sense, since the long variable idGrupoUP cannot be null, so the error is not there either.

I have 1000 active users and I have good reviews (in the Play Store), I have done tests on several devices and they do not present that problem, so I do not understand what happens to those 7 users to whom that has happened. I hope someone can help me, thank you very much.

CodePudding user response:

No matter how it happens, you need to check for nulls in this line:

getIntent().getExtras().getLong("idGroup")

It's telling you that getIntent().getExtras() is NULL in some specific circumstances. It's impossible to tell when it could actually happens because you didn't post your code for the EditGroup class, so we have no way of knowing where and how you're calling that line. But right away I can give you at least 3 possibilities:

  1. If someone is using a custom launcher app, they can create shortcut directly to any activity in your app. So, if they're launching EditGroup for some reason, no extras would ever be sent, and so you'll get a NullPointerException

  2. Again, without seeing your code, I'm just guessing, but there could be a navigation issue. For example, if you launch any activity from your EditGroup class, it's possible that upon returning back to the EditGroup class, it tries to read the intent extras, and at that point they would be NULL

  3. I don't know what your Manifest looks like, but if the EditGroup activity has any intent filters, then it can be launched using those filters. And, again, the extras could easily be NULL

So, as I said, the bottom line is that you have to check that line for NULLs or surround it with try/catch

CodePudding user response:

The null object is the Bundle got from getIntent().getExtras(). The startActivity's process may be injected and retrofited by system, you can pay attention to the device brands and versions which get the error.

CodePudding user response:

Remember to check nullability of getIntent().getExtras()

For better convenience, convert the java code to Kotlin and use following as an example :

intent.extras!!.getInt(EXTRA_ROOM_ID)
intent.extras?.getInt(EXTRA_ROOM_ID) ?: 0
  • Related