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

challenge 2 of task 2

Bummer! Your loop should run three times and call 'getJSONObject()' each time through.

// JSONObject 'jsonData' was loaded from data.json JSONArray jsonShows = jsonData.getJSONArray("jsonShows");

for (int i=0; i<jsonShows.length();i++){ JSONObject show = jsonShows.getJSONObject(i); String title = show.getString("title"); int pages = show.getInt("pages"); Log.i("CodeChallenge",title+","+pages); }

Help. how to make the code loop thrice as bummer wants.

6 Answers

I stated in a previous answer that you should try posting just the title in the log statement instead of title,page

I'll post my code here (it has passed the challenge):

JSONArray jsonShows = jsonData.getJSONArray("shows");

for (int i=0; i<jsonShows.length();i++){ 
  JSONObject jsonData = jsonShows.getJSONObject(i); 
  String title = jsonData.getString("title"); 
  Log.i("CodeChallenge", title);
}

Man you are a genius, it took a whole week to pass this challenge and you just made my coding hopeful.You are a star. thanks a lot. My apology for failing to to figure out what you meant regarding title and page on the log statement.Hats off man.

This might not be correct, because I am not in that stage yet, but, maybe change the 'if' statement to i != 3 and add 1 to i each time that runs

did not solve the problem, thanks for the effort.

Your Welcome!

But, you could tag people like Jon Kussmann and Gloria Dwomoh, they are good at answering questions

Oh yea and, it was very hard to read your code so maybe you can edit he code and add back-ticks like this one `. I is located under the "ESC" key and above the tab key. Add it before and after your code and, after the first set, type in java or the language your dealing with. JAVA for android

I still haven't found anyone who has passed the challenge yet.

Thanks Evering, lem try and hook up with them. thanks once again for your assistance.

Hi Tendai,

You might need to copy the data.json file here so I can check what data you are getting.

First try changing:

for (int i=0; i<jsonShows.length();i++)

To

for (int i=0; i < 3 ;i++)

It could be the length is greater than 3, but you only want the first 3 elements. I would need the data to know for sure. Also please post the original question.

changing to for (int i=0;i<3;i++) results in error ' Bummer! java.lang.NullPointerException ()' however the original challenge is

Challenge Task 2 of 2

Finally, write a 'for' loop that loops through jsonShows. In each step of the loop, use getJSONObject(int index) to get a JSONObject from the array. Then use the Log.i() method (with "CodeChallenge" as the tag) to write the show title. and my code is

// JSONObject 'jsonData' was loaded from data.json JSONArray jsonShows = jsonData.getJSONArray("jsonShows");

for (int i=0; i<jsonShows.length();i++){ JSONObject show = jsonShows.getJSONObject(i); String title = show.getString("title"); int pages = show.getInt("pages"); Log.i("CodeChallenge",title+","+pages); }

the output leads to Bummer! Your loop should run three times and call 'getJSONObject()' each time through. may you help in making the code loop run three times.

// JSONObject 'jsonData' was loaded from data.json JSONArray jsonShows = jsonData.getJSONArray("jsonShows");

for (int i=0; i<3;i++){ JSONObject show = jsonShows.getJSONObject(i); String title = show.getString("title"); int pages = show.getInt("pages"); Log.i("CodeChallenge",title+","+pages); }

gives ; Bummer! java.lang.NullPointerException ()

What if you used the your original code, but instead of putting the title and the pages in the log statement, you just put the titles?

Challenge Task 2 of 2

Finally, write a 'for' loop that loops through jsonShows. In each step of the loop, use getJSONObject(int index) to get a JSONObject from the array. Then use the Log.i() method (with "CodeChallenge" as the tag) to write the show title.

data.json

1 { 2 "name":"Netflix Playlist", 3 "shows":[ 4 { 5 "title":"Doctor Who", 6 "season":8, 7 "episode":3 8 }, 9 { 10 "title":"Arrow", 11 "season":1, 12 "episode":10 13 }, 14 { 15 "title":"Portlandia", 16 "season":4, 17 "episode":5 18 } 19 ] 20 } CodeChallenge.java; my code below

// JSONObject 'jsonData' was loaded from data.json JSONArray jsonShows = jsonData.getJSONArray("jsonShows");

for (int i=0; i<jsonShows.length();i++){ JSONObject jsonData = jsonShows.getJSONObject(i); String title = jsonData.getString("title"); int pages = jsonData.getInt("pages"); Log.i("CodeChallenge", title+","+pages); } output: Bummer! Your loop should run three times and call 'getJSONObject()' each time through.

 JSONArray jsonShows = jsonData.getJSONArray("jsonShows");

Should be :

 JSONArray jsonShows = jsonData.getJSONArray("shows");

I hope that helps.

oooh that part is ok and is for the first challenge and has already passed that one. the issue is on the second challenge where the for loop has to run thrice calling the 'getJSONObject(int index)' each time through. thats where my issue since no errors from my code are being picked by the editor but saying has to run thrice. maybe if you could help startig from the for loop.