1

when I am running the code in debugging ,it's working fine and give the desired output. but when I am running its normal mode ,its crashing and giving above exception. Getting Error when I m add an item through adapter into its BaseClass

HashMap<String,String> mMapWishList = new HashMap<String, String>();
                String id = mProducts.getSingleProductId.get(position);
                String name = mProducts.getSingleProductName.get(position);
                String price = mProducts.getSingleProductPrice.get(position);
                String oldPrice = mProducts.getSingleProductOldPrice.get(position);
                String discount = mProducts.getSingleProductDiscount.get(position);
                String description = mProducts.getSingleProductDescription.get(position);

getting only single item How can I add multiple items ? getting this error LogCatt Error

FATAL EXCEPTION: main
                                                                                                                                     java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
atcom.example.dev.newkhreedapp1.views.adapters.Product_Adapter$1.onClick(Product_Adapter.java:193)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
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)

why it's behaving like this ,not getting any clue. Thanks for any help.

Nithinlal
  • 4,845
  • 1
  • 29
  • 40

1 Answers1

0

The only thing that comes into my mind is that you get your mProducts asynchronously, so during debug it's enough time to receive data, but during run mode it's failing. You should run this code after your products are retrieved from server.
Or another way not to get this error is add a check:

if (mProducts.size() > position) {
    String id = mProducts.getSingleProductId.get(position);
    String name = mProducts.getSingleProductName.get(position);
    String price = mProducts.getSingleProductPrice.get(position);
    String oldPrice = mProducts.getSingleProductOldPrice.get(position);
    String discount = mProducts.getSingleProductDiscount.get(position);
    String description = mProducts.getSingleProductDescription.get(position);
}  

The crash should be gone, but you probably won't have data you need.

Max Zavernutiy
  • 1,771
  • 1
  • 18
  • 27