Search code examples
androidandroid-intentfirebase-realtime-databaseonitemclicklistener

Passing value to another activity via intent putString is crashing app with nullPointerException


I know there are plenty such questions here - but I believe my issue is unique.

I have a listview of modules, when a user selects a module another activity opens showing the 'decks' for that module. This requires the second activity to know which listview item was clicked. The following is how I try to achieve this.

modulesListView.setOnItemClickListener((parent, view, position, id) -> {
        ModuleObject moduleObject = list.get(position);

        Intent intent = new Intent(FlashCards.this, FlashCardDecks.class);
        Bundle extras = new Bundle();
        extras.putString("EXTRA_MODULE_ID", moduleObject.getModuleId());
        extras.putString("EXTRA_MODULE_NAME", moduleObject.getModuleName());
        intent.putExtras(extras);
        startActivity(intent);

    });

In the new activity I access moduleName and Id like so:

Bundle extras = getIntent().getExtras();
    assert extras != null;
    String moduleId = extras.getString("EXTRA_MODULE_ID");
    String moduleName = extras.getString("EXTRA_MODULE_NAME");

I then use the moduleName to setTitle and moduleId to access firebase and fill a listview. All good. The variables are passed as they should be.

But in my second activity I have dialogs that let users add and remove 'decks' from the firebase database using the following reference...

decksDatabaseReference = firebaseDatabase.getReference().child(userID).child("decks").child(moduleId);

Using this reference - a user can add 'decks' to a listview in the second activity

        //create new deckObjet
        DeckObject deckObject = new DeckObject();
        //get unique id from firebase
        String deckId = decksDatabaseReference.push().getKey();
        //set unique id to deck object
        deckObject.setId(deckId);
        //add new deckObject to database
        decksDatabaseReference.child(deckId).setValue(deckObject);
        //refresh activity to show change in listview
        startActivity(new Intent(FlashCardDecks.this, FlashCardDecks.class));

This will crash the app. But when I next open the app, the new deck is there in the second activity. So I don't know why the app is crashing will a null pointer

Here is the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ec16358.examcompanion/com.ec16358.examcompanion.FlashCardDecks}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference

...

at com.ec16358.examcompanion.FlashCardDecks.onCreate(FlashCardDecks.java:48)

(FlashCardDecks line 48 = String moduleId = extras.getString("EXTRA_MODULE_ID"); )


Solution

  • This worked

      if (getIntent().getStringExtra("EXTRA_MODULE_ID") != null && (getIntent().getStringExtra("EXTRA_MODULE_NAME") != null)){
                moduleId = bundle.getString("EXTRA_MODULE_ID");
                moduleName = bundle.getString("EXTRA_MODULE_NAME");
            }