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!
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

Chad Johnson
10,936 Points"ClassCastException:java.lang.String cannot be cast to org.json.JSONObject" when attempting to sign up a new user
When I attempt to sign up a new user, I get this ClassCastException. The user is still signed up after I dismiss the error, but I would like to get it corrected so it doesn't pop up every time a new user signs up. I thought it was something wrong with my code, so I downloaded Ben's files (SignUpActivity.java and FriendsFragment.java) and used them in my project, but I still get the same exception. Any help would be greatly appreciated!
5 Answers

Navod Bopitiya
3,630 PointsUpdate to this. Not sure if it's mentioned anywhere else.
Just insert this else if block before creating the dialog
else if(e.getMessage().equals("java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject")){
//Do nothing since friends are not yet added.
//ignore
}
The dialog always pops up because the Friends Fragment checks for friends when none are added yet. The above else if block should get rid of the error.

Vipul Garg
354 PointsInitially the app was not actually showing this error, I don't know what happened it start showing every time new user signup. I also removed the dialog because of this error and its working fine now.

Joseph Kopko
20,827 PointsGetting the same error, I think it has something to do with the array adapter call inside of the OnResume > done:
'''ArrayAdapter<String> adapter = new ArrayAdapter<String>(EditFriendsActivity.this, android.R.layout.simple_list_item_checked, usernames);'''
The context solution recommends recasting that second String cast as <>, but not sure why. I think that is what is throwing the error when running the program.

Jon Kussmann
Courses Plus Student 7,254 PointsI had the same issue, but just decided to remove the code that made the dialog. You can still log the error. The app will work just fine without that dialog (at least, that's what I've found).

Chad Johnson
10,936 PointsGreat, thanks Jon!