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

I cannot be resolved to a variable

Hello,

I am working on the "Filling our String Array and Creating the Adapter" portion of "Adapting Data for Display in a List". I am having an issue that seems strange to me, so I am hoping that I am just missing something.

The lines of code in question are:

82 try {

83 JSONArray jsonPosts = mBlogData.getJSONArray("posts");

84 mBlogPostTitles = new String[jsonPosts.length()];

85 for (int i = 0; i < jsonPosts.length(); i++);{

86 JSONObject post = jsonPosts.getJSONObject(i);

87 String title = post.getString("title");

88 title = Html.escapeHtml(title).toString();

89 mBlogPostTitles[i] = title;

90 }

Eclipse is telling me that the variable i on lines 86 and 89 has not been initialized, despite line 85. If I initialize the variable again, it will override its value and not satisfy the intent of selecting the next item in the array. What am I missing? Why is this variable not carrying down?

Thanks in advance!

2 Answers

Andrew Pope
Andrew Pope
11,915 Points

You have a semi-colon on line 85 that shouldn't be there - probably just a typo!

Your version is:

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

But it should look like this:

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

Hope this helps!

Andy.

Thanks, that did it... not sure how or why it was there. Mind referring me to where you can type out the code like that? Would probably make asking questions easier in the future. Thanks again.

Andrew Pope
Andrew Pope
11,915 Points

At the bottom of the text box for typing posts there's a little link called "Markdown Cheatsheet" (it's between the text box and the buttons). All info is on there!

Andy