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

Extra 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

When 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.

Wow what a facepalm moment, Thanks!