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 trialDavid Reidy
Courses Plus Student 2,317 PointsIncorrect "correct" answer
import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.widget.EditText;
public class GuessNumberActivity extends Activity {
protected EditText mNumberField; protected String mUserInput;
// onCreate() and other code has been omitted
public void makeGuess() {
// Set mUserInput here!
mUserInput = mNumberField.getText().toString();
// Check to see if the user's number is correct
boolean isCorrect = GuessEngine.testGuess(mUserInput);
if (isCorrect)
{
AlertDialog.Builder builder = new AlertDialog.Builder(GuessNumberActivity.this);
builder.setTitle("Hooray!");
builder.setMessage("You did it!");
AlertDialog dialog = builder.create();
dialog.show();
}
}
}
The code above works as a correct answer for the challenges, yet in practice it would fail as mNumberField is never initialised and hence would still be null when used in the mUserInput = mNumberField.getText().toString(); line.
Adding in mNumberField = (EditText)findViewById(R.id.numberField); results in a wrong answer.
2 Answers
Ben Jakuben
Treehouse TeacherHi David,
Good observation! Note the comment, though:
// onCreate() and other code has been omitted
Some code from Code Challenges is omitted to keep it easier to navigate and focus on the important parts of the challenge. I'll take a look at making that more clear, though. :-)
David Reidy
Courses Plus Student 2,317 PointsHi Ben,
Yes, that makes sense, I just didn't think that as I'd declared mNumberField and it wasn't mentioned elsewhere in the supplied code that it would be in the omitted code, but then I hadn't considered the omitted code at all really.
Thanks for getting back to me, I'm quite enjoying the course.
All the best,
David.
Ben Jakuben
Treehouse TeacherGlad to hear it! Thanks for the feedback, and thanks for being a Treehouse student! :)