Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another?

ANdr

I am trying to get an arraylist in ScanLocate activity from an UpdateLocation activity.

I'm using startActivityForResult method to call the scan method which populates the ArrayList wifiList, I then want to send the ArrayList to the Update Location class.

I start by calling startActivityForResult in Update Location:

private void getScan(){
    //Create an intent to start ScanLocate
    final Intent i = new Intent(this, ScanLocate.class);
    //Start scanLocate with request code
    startActivityForResult(i, REQUEST_READINGS);
}

Next, in ScanLocate I created the sendData method (note: the check confirms that the ArrayList data is intact at that point):

private void sendData(){
    //create a new intent as container for the result
    final Intent readings = new Intent(this, UpdateLocation.class);

    //check that data is in wifiList
    for(String s:wifiList){
        Log.v(TAG,"List Items: " + s);
    }

    //create bundle for string array
    Bundle b = new Bundle();
    b.putStringArrayList(key, wifiList);

    //add readings to send to updateLoc
    readings.putExtras(b);

    //set code to indicate success and attach Intent
    setResult(RESULT_OK, readings);

    //call finish to return
    finish();
}

The final part is back in UpdateLocation with the onActivityResult:

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent readings){
    super.onActivityResult(requestCode, resultCode, readings);

    //check if request code matches the one set above
    if(requestCode == REQUEST_READINGS){

        //check if sendData() in ScanLocate was successful
        if(resultCode == RESULT_OK){
            //get the readings from the Intent
            Log.v(TAG, "HERE");
            result = getIntent().getExtras().getStringArrayList(ScanLocate.key);
            Log.v(TAG, "HERE2");

            for(String s : result) {
                Log.v(TAG, "Location 5: " + s);
            }
        }else{
            //ScanLocate was unsuccessful
        }
    }
}

The first "Here" is displayed however it then falls down on the next line, getStringArrayList() throws a null pointer exception.

I have looked through the documentation and at previous questions on here and I cannot see what is going wrong.

Any advice would be appreciated, thanks.

Previous questions:

startactivityforResult not working

How to pass an ArrayList to the StartActivityForResult activity

Juan Cruz Soler

You don't need to call getIntent(), use the parameter provided by the method:

result = readings.getStringArrayListExtra(ScanLocate.key);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I getting an error for 'Column cannot be null' during my post api when I am passing a value from a html form

I am getting null object reference error whiile passing data to one fragment to another

Why I am getting null value from list which is passed from another Activity?

Class Cast Exception when passing array of Serializables from one activity to another

Why am I getting a null pointer access?

I am getting Null pointer Exception while fetching data from the excel sheet

Servet - Why am i getting null pointer exception?

I am getting Null Pointer Exception in GridView Android

Why am I getting a null pointer exception in my iterator?

Null-Pointer Exception when trying to access an EditText from another activity in Kotlin

Why am I getting a Null Pointer Exception when reading user data from Firebase?

Why am I NOT getting a Null Reference Exception?

Why am i getting segmentation fault when passing a pointer as a function parameter?

Getting null values when passing data from one activity to another in android

Why am i NOT getting null pointer exception in this case while adding null to a list and sorting it

Null Point Exception when I call method from another Activity

why am i getting null pointer exception when trying to set a value to "rating" in my rating bar?

Passing object from one Activity to another activity using GSON lead to null pointer exception

I am getting null pointer exception when I an trying to putExtra from intent

I am getting null pointer exception when i tried to download file from dropbox

I am struggling finding out why am I getting a null pointer exception. I know it's because of the null but I can't seem find which one

getting error when passing data from one activity to another activity

I am getting Null pointer exception when i run my app 2nd time

passing data from an activity to another and getting an error when passing and EditText

"Intent must not be null" when sending strings from one activity to another

Why am I getting a Null Pointer Exception Error on Runtime as soon as i run my app, it crashes?

Why am I getting a null pointer exception for my object and how can I fix it?

My app is getting a null pointer exception immediately after i enter into the register activity, I am unable to figure out the reason

why am I getting Null Pointer Exception while trying to check if the username is already present in the database?