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 Build a Simple Android App (2014) Coding the Fun Facts Using an Array

Jason Losito
Jason Losito
Courses Plus Student 8,865 Points

Do you really need to put a blank value when initializing a String variable or any other variable?

I tried typing just String bestSport; and there was an error saying that the bestSport variable has not been initialized.

1 Answer

The Code Challenge asked you to declare and initialize the variable bestSport, so that's why you're getting the error. In a normal Java compiler, just declaring a variable would not cause any errors (but you'll want to initialize it before you use it).

When it's checking to see if your answer is correct, it uses the variable bestSport, but since you didn't initialize it, you get that error:

JavaTester.java:26: variable bestSport might not have been initialized
if (bestSport instanceof String && bestSport == sports[0]) {
1 error

Jason Losito
Jason Losito
Courses Plus Student 8,865 Points

Thank you answering my question. It is clear to me now.