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
MUZ141098 Tendai Matemera
4,318 Pointschallenge 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
Jon Kussmann
Courses Plus Student 7,254 PointsI 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);
}
Farouk Charkas
1,957 PointsThis 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
MUZ141098 Tendai Matemera
4,318 Pointsdid not solve the problem, thanks for the effort.
Farouk Charkas
1,957 PointsYour Welcome!
Farouk Charkas
1,957 PointsBut, you could tag people like Jon Kussmann and Gloria Dwomoh, they are good at answering questions
Farouk Charkas
1,957 PointsOh 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
MUZ141098 Tendai Matemera
4,318 PointsI still haven't found anyone who has passed the challenge yet.
MUZ141098 Tendai Matemera
4,318 PointsThanks Evering, lem try and hook up with them. thanks once again for your assistance.
Jon Kussmann
Courses Plus Student 7,254 PointsHi 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.
MUZ141098 Tendai Matemera
4,318 Pointschanging 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.
MUZ141098 Tendai Matemera
4,318 Points// 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 ()
Jon Kussmann
Courses Plus Student 7,254 PointsWhat 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?
MUZ141098 Tendai Matemera
4,318 PointsChallenge 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.
Jon Kussmann
Courses Plus Student 7,254 Points JSONArray jsonShows = jsonData.getJSONArray("jsonShows");
Should be :
JSONArray jsonShows = jsonData.getJSONArray("shows");
I hope that helps.
MUZ141098 Tendai Matemera
4,318 Pointsoooh 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.
MUZ141098 Tendai Matemera
4,318 PointsMUZ141098 Tendai Matemera
4,318 PointsMan 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.