java - ParseQuery - Properly handling IndexOutOfBoundsException - Parse.com? -


how 1 handle throwindexoutofboundsexception? basically, i'm querying user. if user exists, shows profile. however, if no entry found crashes app , throws exception. should put in "else" statement app doesn't crash?

                    @override                     public void done(list<parseuser> parseusers, parseexception e) {                             if (e == null) {                                     // query successful.                                     parseuser user = parseusers.get(0);                                     string userid = user.getobjectid();                                     showprofileactivity(userid);                              } else {                                 // query unsuccessful.                              }                         }                     }); 

here logcat:

10-14 21:27:06.888  28595-28595/com.app.social e/androidruntime﹕ fatal exception: main     process: com.elgami.customizer, pid: 28595     java.lang.indexoutofboundsexception: invalid index 0, size 0             @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255)             @ java.util.arraylist.get(arraylist.java:308)             @ com.elgami.feed.searchactivity$1$1.done(searchactivity.java:74)             @ com.elgami.feed.searchactivity$1$1.done(searchactivity.java:67)             @ com.parse.parse$6$1.run(parse.java:944)             @ android.os.handler.handlecallback(handler.java:739)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:148)             @ android.app.activitythread.main(activitythread.java:5417)             @ java.lang.reflect.method.invoke(native method)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 

you can check if list empty :

 @override       public void done(list<parseuser> parseusers, parseexception e) {                                 if (!parseusers.isempty()) {                                         // query successful.                                         parseuser user = parseusers.get(0);                                         string userid = user.getobjectid();                                         showprofileactivity(userid);                                  } else {                                     // query unsuccessful.                                  }                             }                         }); 

or if want catch exception use try -catch :

 @override   public void done(list<parseuser> parseusers, parseexception e) {           if (e==null) {             // query successful.           try{                parseuser user = parseusers.get(0);                string userid = user.getobjectid();              }        catch(arrayindexoutofboundsexception e)            {        // print message user not exist .              }              showprofileactivity(userid);             }                else {                    // query unsuccessful.                      }                        }                             }); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -