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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![tshuutheniemvula](https://secure.gravatar.com/avatar/db7e6e677826aaebc17bc7ffb2b864f8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
tshuutheniemvula
7,251 PointsExtra Credits Array Madness
So I set up some code like this, guessing I'm wrong since I get mad compile errors. Is it that I'm providing incorrect parameters for SetText?
public class TextViewArrayMadness extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view_array_madness);
String[] arrays = {"1","2","3","4","5"};
TextView window1 = (TextView)findViewById(R.id.textView);
TextView window2 = (TextView)findViewById(R.id.textView2);
window1.setText(arrays[1] + arrays[2]+ arrays[3]+ arrays[4]+ arrays[5]);
window2.setText(arrays[5] + arrays[4]+ arrays[3]+ arrays[2]+ arrays[1]);
}
2 Answers
![Kristen Law](https://uploads.teamtreehouse.com/production/profile-photos/1145562/micro_Screen_Shot_2014-08-20_at_10.19.33_PM.png)
Kristen Law
16,244 PointsWhen you're accessing the values in arrays
, remember that the index starts at 0. So arrays[0]
would be the string "1", arrays[1]
would be "2", and so on. You are trying to access arrays[5]
which is not valid. There is no string at index 5 within arrays
.
![tshuutheniemvula](https://secure.gravatar.com/avatar/db7e6e677826aaebc17bc7ffb2b864f8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
tshuutheniemvula
7,251 PointsWow what a facepalm moment, Thanks!