0

I'm trying to get the object saved in parse cloud but it's giving me

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

the code is:

      private void parsingTask() {
    //po=new ParseObject("LatLng");
    ParseQuery<ParseObject> query=ParseQuery.getQuery("LatLng");
    query.whereEqualTo("imei_code","357146054169800");
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> latlanglist, ParseException e) {
            if(e==null){
        String firstItemId=latlanglist.get(0).getObjectId();
        lat=latlanglist.get(0).getString("latitude");
        lang=latlanglist.get(0).getString("longitude");
        e1.setText(lat);
        e2.setText(lang);
        e3.setText(firstItemId);

            Toast.makeText(getApplicationContext(),"Found the New Location:\nLatitude:"+lat+
                    "\nLongitude:"+lang+"\nObjectID:"+firstItemId,Toast.LENGTH_LONG).show();
            }
            else{

            Toast.makeText(getApplicationContext(), "Error was found\n"+e,Toast.LENGTH_LONG).show();

            }


        }
    });
    // TODO Auto-generated method stub

}

the logcat output:

01-15 14:34:21.064: E/AndroidRuntime(10986): FATAL EXCEPTION: main
01-15 14:34:21.064: E/AndroidRuntime(10986): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-15 14:34:21.064: E/AndroidRuntime(10986):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at java.util.ArrayList.get(ArrayList.java:304)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at com.robotrackerserver.MainActivity$1.done(MainActivity.java:43)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at com.parse.FindCallback.internalDone(FindCallback.java:45)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at com.parse.FindCallback.internalDone(FindCallback.java:31)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at com.parse.Parse$6$1.run(Parse.java:973)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at android.os.Handler.handleCallback(Handler.java:615)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at android.os.Looper.loop(Looper.java:137)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at android.app.ActivityThread.main(ActivityThread.java:4944)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at java.lang.reflect.Method.invokeNative(Native Method)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at java.lang.reflect.Method.invoke(Method.java:511)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-15 14:34:21.064: E/AndroidRuntime(10986):    at dalvik.system.NativeStart.main(Native Method)
01-15 14:34:21.134: D/dalvikvm(10986): GC_CONCURRENT freed 311K, 15% free 7079K/8327K, paused 21ms+4ms, total 118ms
3
  • please send your logcat output Commented Jan 15, 2015 at 9:02
  • what if the list is empty? then you just try to get the first element and it crashes. use if(latlanglist != null && !latlanglist.isEmpty()) before latlanglist.get(0) Commented Jan 15, 2015 at 9:03
  • Included the logcat output.in the question Commented Jan 15, 2015 at 9:09

2 Answers 2

3

The exception means that the returned list, List<ParseObject> latlanglist, is empty. Instead of checking if the exception is null, you should check the constraints on the object you have to work on.

  if(latlanglist != null && !latlanglist.isEmpty()){

  }
Sign up to request clarification or add additional context in comments.

7 Comments

OK but why I am unable to get the object?
there has to be a mistake in your query
Should I have to make the class wrt the class made in the parse cloud??Because every tutorial is following this aproach?I am trying to get the object directly.?
I put the condition and you were right.List is empty.
Sorry Sharky..Because the error was solved with this condition and my question was also wrt to this error.thats why I marked this correct.And I dont know that comments can be marked right or not.
|
0

Try this

if(e==null&& latlanglist!=null){
if(latlanglist.size()>0){
String firstItemId=latlanglist.get(0).getObjectId();
lat=latlanglist.get(0).getString("latitude");
lang=latlanglist.get(0).getString("longitude");
e1.setText(lat);
e2.setText(lang);
e3.setText(firstItemId);

Toast.makeText(getApplicationContext(),"Found the New Location:\nLatitude:"+lat+
"\nLongitude:"+lang+"\nObjectID:"+firstItemId,Toast.LENGTH_LONG).show();
}
else{

Toast.makeText(getApplicationContext(), "Error was found\n"+e,Toast.LENGTH_LONG).show();

}

}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.