Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Using Parcelable Data Retrieving Parcelable Data

Aldrin Cabuniag
Aldrin Cabuniag
3,783 Points

Error: Attemp to get length of Null Array

Currently, trying to follow along the Stormy Weather App on the Retrieving Parcelable Array bit however I am getting an error:

NullPointerException: Attempt to get length of null array at com.teamtreehouse.stormy.ui.DailyForecastActivity.onCreate(DailyForecastActivity.java:24)

My code on line 24 is:

mDays = Arrays.copyOf(parcelables, parcelables.length, Day[].class);

Code that surrounds line 24:

public class DailyForecastActivity extends ListActivity { private Day[] mDays;

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

    Intent intent = getIntent();
    Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.DAILY_FORECAST);
    mDays = Arrays.copyOf(parcelables, parcelables.length, Day[].class);

    DayAdapter adapter = new DayAdapter(this, mDays);
    setListAdapter(adapter);

}

I've attempted to refer to a similar question, however the question wasn't followed up therefore no answers. I'm not sure how to check if my parcel arrays are returning "nulls". Any tips would be appreciated :) thanks.

Aaron Goodman
Aaron Goodman
3,953 Points

I'm having the same issue. My code in DailyForecastActivity is the same as above, my code providing the intent( with the parcelable) is below:

 @OnClick (R.id.dailyButton)
    public void startDailyActivity(View view) {
        Intent intent = new Intent(this, DailyForecastActivity.class);
        intent.putExtra(DAILY_FORECAST, mForecast.getDailyForecast());
        startActivity(intent);
    }

1 Answer

Simon Coates
Simon Coates
28,694 Points

Error might suggest that

Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.DAILY_FORECAST);

is faulty, or the code that should provide the Parcelable is faulty (you haven't shown the code that puts it on the intent). The short is that the above isn't returning anything (ie. is null).