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 trialSlava Fleer
6,086 PointsI trying to recreate my own messenger based on this course. I have strange problem. My app crashes sometimes...
First, it is my app till now.
https://github.com/slavafleer/myMessenger
After I created removing the buddies from buddies list, while app checking for adding markers on users from buddies list, the app sometimes crashes with NullPointerException
04-27 10:54:33.391 11763-11763/com.slava.mymessenger D/AndroidRuntime﹕ Shutting down VM
04-27 10:54:33.391 11763-11763/com.slava.mymessenger W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41e50c08)
04-27 10:54:33.396 11763-11763/com.slava.mymessenger E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.slava.mymessenger, PID: 11763
java.lang.NullPointerException
at com.slava.mymessenger.ui.EditBuddiesActivity$3.done(EditBuddiesActivity.java:148)
at com.slava.mymessenger.ui.EditBuddiesActivity$3.done(EditBuddiesActivity.java:144)
at com.parse.Parse$6$1.run(Parse.java:944)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
line 148: for(int i = 0; i < mUsers.size(); i++) {
but when i am pressing ok button on device for approving it returning me back to MainActivity, like it was no crashes and after it I sometimes can to enter to Edit Buddies Menu and all works as need to be.
So how to find the bug?
Thanks for any help.
2 Answers
James Simshaw
28,738 PointsHello,
I'm not 100% sure if this is the issue, but it looks like you have a race condition in your code. You are calling mUsers.size() possibly before mUsers is set. You have the initialization of mUsers inside the done method for your query which is getting ran in the background. However, you have your call to addBuddiesCheckMarks() after the query so this is getting ran while the query is being executed. So sometimes, you might get lucky and mUsers gets set from the query before it is referenced in addBuddiesCheckMarks(). The better solution is to include the call to addBuddiesCheckMarks() within the done call. So it should look like
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
EditBuddiesActivity.this,
android.R.layout.simple_list_item_checked, usernames);
mUsersList.setAdapter(adapter); // <-----This is line 126
addBuddiesCheckMarks();
Slava Fleer
6,086 PointsHI, James. If you don't mind I have another similar problem.
My code: https://github.com/slavafleer/myMessenger.git
What I found, that while I changing fast between fragments, the list doesn't loaded in time and its crushes. At least that what I think happening.
04-29 13:56:22.041 18800-18800/com.slava.mymessenger E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.slava.mymessenger, PID: 18800
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.slava.mymessenger.ui.TeteATeteFragment$1.done(TeteATeteFragment.java:67)
at com.slava.mymessenger.ui.TeteATeteFragment$1.done(TeteATeteFragment.java:51)
at com.parse.Parse$6$1.run(Parse.java:944)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
What are the solution in this case? Do I need to add some delay, or use try/catch ? Thanks for you time.
p.s. Off cause if somebody else know solution , I will be grateful for it.
James Simshaw
28,738 PointsI'm not sure I can offer too much help on the specifics of what you are doing at the moment since the code on your github for TeteATeteFragment.java only contains 21 lines, yet the error occurrs with line 67. Though this is looking like another issue with asynchronous tasks from the error message.
Slava Fleer
6,086 PointsJames. You checked old branch. Please check toTreeHouse2 branch. thanks again.
James Simshaw
28,738 PointsI'm not sure about if this will help, but here on stackoverflow, they recommend putting most of the code you have in onResume() into onActivityCreated() which is also called every time the fragment is brought back to life, but you apparently know you should have all of your view items there to manipulate.
Slava Fleer
6,086 PointsSlava Fleer
6,086 PointsWow. Thanks a lot. This is it.
Need to burn in the brain that when you are working with something remote, you need 100% sure that it downloaded 100% =)
Again, thank you.