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
Steve Yoon
2,408 PointsCode Challenge: All About @string Resources (part 5)
I keep getting the same error: "Bummer! Compilation Error!" I'm pretty sure I'm doing it right though...
Resources resources = getResources();
phoneNumberLabels = resources.getStringArray(R.array.phoneNumbers);
String[] phoneNumberLabels = { "-home", "-work", "-mobile" };
Button saveButton = (Button) findViewById(R.id.saveButton);
String label = getString(R.string.saveLabel);
saveButton.setText(label);
Help would be greatly appreciated. Thank you in advance!
6 Answers
Maximilian McKinley
7,525 PointsYou can change your code one of two ways.
Option one: Change Resources resources = getResources(); to --> String resources = getResources();
Option two: Delete line one entirely and change phoneNumberLabels = resources.getStringArray(R.array.phoneNumbers); to -- > phoneNumberLabels = getResources().getStringArray(R.array.phoneNumbers);
Steve Yoon
2,408 PointsThanks for the response, but (unfortunately) it didn't work. I tried both, but it said the same thing; Bummer! Compilation Error! Since it's a challenge, maybe it's expecting a specific answer.
Maximilian McKinley
7,525 PointsThat is strange since I specifically went back and did that code challenge over and this is what I used to pass it....
Steve Yoon
2,408 PointsMaybe it's some sort of glitch.
Jonathan Baker
2,304 PointsSteve,
It looks like you're assigning the String[] to phoneNumberLabels before the the variable is declared.
Swap these two lines and you should be good to go. =)
phoneNumberLabels = resources.getStringArray(R.array.phoneNumbers);
String[] phoneNumberLabels = { "-home", "-work", "-mobile" };
Steve Yoon
2,408 PointsThanks Jonathan! It worked. One thing though; how did that make a difference? I don't think that those two specific lines' order should've mattered. Anyway, thank you very much. This one has been blocking me for a while. :D
Jonathan Baker
2,304 PointsIt matters because what you had is invalid syntax and would (should) throw a Java compiler error (which it did). You can not assign a value to a variable before it has been declared (in Java).
For example:
// This will throw a compilation error!
myVariable = "Hello Compilation Error";
String myVariable;
// This will work perfectly!
String myVariable;
myVariable = "Hello World!";
Glad I could help. If you wouldn't mind, please select my / an answer as the correct one so other people see this question has been answered! =)
Jonathan Baker
2,304 PointsIn hindsight, you could also have simply replaced the static String[] initializer with the result of the resource.
String[] phoneNumberLabels = resources.getStringArray(R.array.phoneNumbers);