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
Joe Champion
946 PointsProperties of arrays exercise?
When I create the String [] facts array on line 27 , 'facts' remains grayed out and when I select it I get error "Variable 'facts' is never used." When 'facts' is used later in the code get "cannot resolve symbol 'facts' errors. Also get "cannot resolve symbol 'setText' " in line : factLabel.setText.(fact);
I downloaded the project files anf my FunFactsActivity.java is exactly the same.
2 Answers
Joe Champion
946 PointsHere's my code. The 'X' at beginning of the lines mark where I'm getting errors described in my question. Thanks much!
final TextView factLabel = (TextView) findViewById(R.id.factTextView); Button showFactButton = (Button)findViewById(R.id.showFactButton); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) {
X String[] facts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you wil have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is seriously disturbed.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the Earth when the Great Pyramid was being built."
};
}
// The button was clicked, so update the fact label with a new fact.
String fact = "";
// Randomly select a fact
Random randomGenerator = new Random(); // Construct a new Random number generator object
int randomNumber = randomGenerator.nextInt(facts.length);
// fact = randomNumber + ""; Deleted after challenge
/* Convert randomNumber to a TEXT fact
// if randomNumber equals 0 then
if(randomNumber == 0){
// set fact equal to ants fact
fact = "Ants stretch when they wake up in the morning.";
}
else if(randomNumber == 1){
fact = "Ostriches can run faster than horses";
}
else if (randomNumber == 2){
fact = "Olympic gold medals are actually made mostly of silver.";
}
else {
fact = "Sorry, there was an error!";
}
*/
X fact = facts[ randomNumber] ;
// Update the label with our dynamic fact
X factLabel.setText(fact);
};
showFactButton.setOnClickListener(listener );
}
Jon Kussmann
Courses Plus Student 7,254 PointsIt's a little difficult to tell because the formatting is a little off, but I'll give it a shot.
It looks like you define your facts array inside your onClick method. You close your facts array with "};" which is fine, but then you have another "}" a couple of lines down. That would end your onClick method and so the rest of the lines where you would want to utilize the random number generator would not be called.
Joe Champion
946 PointsThanks Jon, you're the man! Those pesky misplaced curly braces got me. I thought code debug would find that type of error, but instead it told me to check my array and variable names, and the program wouldn't run. Guess you can't expect Android Studio to do EVERYTHING for you? :)
Thank you, Jon, again.
Joe
Jon Kussmann
Courses Plus Student 7,254 PointsNo problem!
Some errors are easy to find/fix.... and some can take a long time. I once spent a week on a single error, only to find it wasn't my code... it was the device I was testing on. XD
Jon Kussmann
Courses Plus Student 7,254 PointsJon Kussmann
Courses Plus Student 7,254 PointsCan you post your code here? It's a little difficult to tell what's going on from your description.