Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,693 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).