Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.