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 Updating the Daily Forecast UI

FAIZEL KHAN
FAIZEL KHAN
2,328 Points

I am getting error "Attempt to get length of null array"

public class Main2Activity extends ListActivity { private Day[]mDays; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent();

    Parcelable[] parcebles= intent.getParcelableArrayExtra(MainActivity.DAILY_FORCAST);
  mDays = Arrays.copyOf(parcebles,parcebles.length,Day[].class);//Day it shows us what type of array we are going to use
    DayAdapter dayAdapter = new DayAdapter(this,mDays);
    setListAdapter(dayAdapter);
}

}

when i run this Caused by: java.lang.NullPointerException: Attempt to get length of null array at com.csing1s.stormy.ui.Main2Activity.onCreate(Main2Activity.java:26) at android.app.Activity.performCreate(Activity.java:5958) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:155)  at android.app.ActivityThread.main(ActivityThread.java:5696)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)

3 Answers

Abhinav Kanoria
Abhinav Kanoria
7,730 Points

Probably there is a problem with parceling(marshalling and unmarshallng) the data. So I think the 'parcebles' array that you declared is empty or is being returned null values. Can you post the code of the 'Day' class where I believe you implemented 'Parcelable' interface?

Be sure to check if you havent commented out the line where you set the daily forecast array. It is in this method:

private Forecast parseForecastData(String jsonData) throws JSONException{

    Forecast forecast = new Forecast();
    forecast.setCurrent(getCurrentDetails(jsonData));
    forecast.setDailyForecast(getDailyForecast(jsonData));
    //forecast.setHourlyForecast(getHourlyForecast(jsonData));

    return forecast;

}

After that you would be fine. This error is indicating that the array you've passed as a parameter is null.

Zachary Rolland
Zachary Rolland
16,465 Points

A lot of people are having this issue.

Make sure you set the daily forecast in MainActivity parseForecastDetails

forecast.setDailyForecast(getDailyForecast(jsonData));