Facebook for Android error on getting gender of user -


some users getting force close when signing facebook in our app. received stacktrace on lines below:

java.lang.stringindexoutofboundsexception: length=0; regionstart=0; regionlength=1 @ java.lang.string.startendandlength(string.java:588) @ java.lang.string.substring(string.java:1475) @ com.yolify.android.activity_splash$7.oncompleted(activity_splash.java:483) @ com.facebook.request$1.oncompleted(request.java:281) @ com.facebook.request$4.run(request.java:1666) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5146) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:732) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:566) @ dalvik.system.nativestart.main(native method) 

code:

string gender = user.getproperty("gender") == null ?  "" :  user.getproperty("gender").tostring(); string gender = gender == null ? "" : gender.substring(0,1).touppercase(locale.english)+gender.substring(1); 

the force close happens on second line of code. since happens in 1 in 100 signups, can't reproduce issue. if gender not null it?

"if gender not null it?" : null turn empty string. indexoutofboundsexception comes trying substring(1) on empty string.

the problem way have used 2 back-to-back ternary operations.

the first ternary operation

string gender = user.getproperty("gender") == null ? "" : user.getproperty("gender").tostring() 

is saying

if(user.getproperty("gender") == null){     gender=""; } else{     gender=user.getproperty("gender").tostring(); } 

so far good. second ternary operation (this gets confusing. dont know why wrote 'string gender'; think should replace 'gender')

string gender = gender == null ? "" : gender.substring(0,1).touppercase(locale.english)+gender.substring(1) 

is saying

if(gender == null){     gender = ""; //gender instead of string gender } else{     gender = gender.substring(0,1).touppercase(locale.english) + substring(1) } 

note first condition false because of first ternary operation. second ternary operation throw indexoutofboundsexception in event used facebook account null gender because first ternary operation makes empty string , second ternary operation trying substring of empty string. easy way test create new fb account , not fill in gender , sign app.

the fix: tend avoid ternary operations plague because though saves lines of code, easy confused when have 2 back deal same variables. replace 2nd ternary operation

if(gender != "") { // gender exists     gender = gender.substring(0,1).touppercase(locale.english); } else{     // gender "" whatever } 

thats "most correct way" think of handling this. try getting rid of +substring(1) in second ternary operation , rid of error thats not recommend.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -