0

How does one properly handle throwIndexOutOfBoundsException? Basically, I'm querying for a user. If the user exists, it shows their profile. However, if no entry is found it crashes my app and throws the exception. What should I put in my "else" statement so that the app doesn't crash?

                    @Override
                    public void done(List<ParseUser> parseUsers, ParseException e) {
                            if (e == null) {
                                    // The query was successful.
                                    ParseUser user = parseUsers.get(0);
                                    String userId = user.getObjectId();
                                    showProfileActivity(userId);

                            } else {
                                // The query was unsuccessful.

                            }
                        }
                    });

Here is the 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 is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at com.elgami.feed.SearchActivity$1$1.done(SearchActivity.java:74)
            at com.elgami.feed.SearchActivity$1$1.done(SearchActivity.java:67)
            at com.parse.Parse$6$1.run(Parse.java:944)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 Answers1

2

you can check if the list is empty :

 @Override
      public void done(List<ParseUser> parseUsers, ParseException e) {
                                if (!parseUsers.isEmpty()) {
                                        // The query was successful.
                                        ParseUser user = parseUsers.get(0);
                                        String userId = user.getObjectId();
                                        showProfileActivity(userId);

                                } else {
                                    // The query was unsuccessful.

                                }
                            }
                        });

or if you want to catch the Exception use try -catch like this :

 @Override
  public void done(List<ParseUser> parseUsers, ParseException e) {
          if (e==null) {
            // The query was successful.
          try{
               ParseUser user = parseUsers.get(0);
               String userId = user.getObjectId();
             }
       catch(ArrayIndexOutOfBoundsException e)
           {
       // Print message for user does not exist . 
            }
             showProfileActivity(userId);
            } 
              else {
                   // The query was unsuccessful.
                     }
                       }
                            });
WannaBeGeek
  • 979
  • 14
  • 32
  • 1
    I was supposed to say the same – Apurva Oct 15 '15 at 04:45
  • 1
    Great! I went for the first option, checking for an empty list. Is the try/catch method more or less common, and are there any particular considerations for using one over the other in terms of best practices? Either way, worked wonderfully. – drearypanoramic Oct 15 '15 at 07:39
  • 2
    catching Exception is great if we want to display the specific message to the user depending upon particuler exception . But they make the program a bit slower . Whereas by checking for Exceptions beforehand like in Option 1 , we prevented it from arising later on in our code . It is better way as it will be faster than Option 2. Read More here : https://docs.oracle.com/javase/tutorial/essential/exceptions/advantages.html and http://stackoverflow.com/questions/299068/how-slow-are-java-exceptions . – WannaBeGeek Oct 15 '15 at 07:55