This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Android and the fly likes how to solve arrayindexoutofbound exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "how to solve arrayindexoutofbound exception" Watch "how to solve arrayindexoutofbound exception" New topic
Author

how to solve arrayindexoutofbound exception

Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
hi,
iam getting this exception at logcat can anyone please help me solve it....i tried hard but could resolve

iam tryingg to get a listview of image and text view toghtr where i get this exception
the first time i load the listview i dont face any problem,later when i go back and load again it gives exception
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

Please post the code.
Maybe I can help you!!!
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
By not accessing array indices that are higher than allowed for the array in question. If an array has n elements, then n-1 is the highest array index you can use.
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

Yes, I was going to see those problems when she would post the code
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
More important than anyone pointing right at the problem is the understanding of what this exception means, and how one can guard against it in the future (and how to debug it if one isn't sure why it happens).
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213



this is where i get arrayindexoutofbound exception
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
So: where do you think the problem happens? You didn't post the top of the stack trace, which is where the most important information is - namely, in which line of code.

The only array seems to be SongsActivity1.myArr, so that's likely where the problem is. And the code is accessing it with an index that is too large. What do you think has happened? *How* could it have happened?
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
here is my code...which gives a listview with imageview and text


i get the proper output when first time the list is created....later when i add an item then it gives arrayindexoutofbound exception...
can anyone suggest...i have pasted the adapter for the listview where actually m stuck....
please suggest help
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
can someone please get me a solution....searching solution since so many days....this arrayindex exception will make me mad n my application will crash....

looking for help...
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
the listview displays as many time as i want without any problem but wen i add a new element to the arraylist and then go to load the listview it gives me arrayindexoutofbound exception.....found no solution anywhere
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

Did you read what Tim said?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
In which line of code does the exception happen? What is the value of the index that causes the problem? What is the length of the array? These are all runtime issues that you need to work out, just looking at code isn't going to help much.
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
the problem is in line 53,56..length of the array depends upon the values to arraylist added at runtime..
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
the problem is here if(s.equals(values[position])) get the exception here
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
03-06 13:30:08.772: W/System.err(571): java.lang.ArrayIndexOutOfBoundsException
03-06 13:30:08.772: W/System.err(571): at com.ixifoto.albums.NewMobileAdapter.getView(NewMobileAdapter.java:94)
03-06 13:30:08.772: W/System.err(571): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
03-06 13:30:08.772: W/System.err(571): at android.widget.ListView.makeAndAddView(ListView.java:1745)
03-06 13:30:08.781: W/System.err(571): at android.widget.ListView.fillDown(ListView.java:670)
03-06 13:30:08.781: W/System.err(571): at android.widget.ListView.fillFromTop(ListView.java:727)
03-06 13:30:08.790: W/System.err(571): at android.widget.ListView.layoutChildren(ListView.java:1598)
03-06 13:30:08.790: W/System.err(571): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
03-06 13:30:08.790: W/System.err(571): at android.view.View.layout(View.java:7175)
03-06 13:30:08.790: W/System.err(571): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
03-06 13:30:08.790: W/System.err(571): at android.view.View.layout(View.java:7175)
03-06 13:30:08.790: W/System.err(571): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-06 13:30:08.790: W/System.err(571): at android.view.View.layout(View.java:7175)
03-06 13:30:08.790: W/System.err(571): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
03-06 13:30:08.790: W/System.err(571): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
03-06 13:30:08.790: W/System.err(571): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
03-06 13:30:08.790: W/System.err(571): at android.view.View.layout(View.java:7175)
03-06 13:30:08.790: W/System.err(571): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-06 13:30:08.790: W/System.err(571): at android.view.View.layout(View.java:7175)
03-06 13:30:08.790: W/System.err(571): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
03-06 13:30:08.790: W/System.err(571): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-06 13:30:08.790: W/System.err(571): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 13:30:08.790: W/System.err(571): at android.os.Looper.loop(Looper.java:123)
03-06 13:30:08.800: W/System.err(571): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 13:30:08.800: W/System.err(571): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 13:30:08.800: W/System.err(571): at java.lang.reflect.Method.invoke(Method.java:507)
03-06 13:30:08.800: W/System.err(571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 13:30:08.800: W/System.err(571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 13:30:08.800: W/System.err(571): at dalvik.system.NativeStart.main(Native Method)
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
my adapter class is
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Tim Moores wrote:What is the value of the index that causes the problem? What is the length of the array? These are all runtime issues that you need to work out, just looking at code isn't going to help much.
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
the problem is here if(s.equals(values[position])) get the exception here

the items are added at runtime
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Do you really not understand why we can't do anything without the information I'm asking for? Actually, once you have that information, you are in a position to address the problem yourself.
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
its says invalid index 6,size is 0
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
So?

The problem is still the same I mentioned in my very first post:
By not accessing array indices that are higher than allowed for the array in question.
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
iam not geting arrayindexoutofbound exception now...but when i add element is not getting displayed in the listview....the size of string is same as size of images array here..it should display the image for the string value...but here it is displaying the image i select over other string value
please suggest...any idea
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You should start a new topic for unrelated questions, lest it gets confusing.
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
sory for the wrong post
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Tim Moores wrote:You should start a new topic for unrelated questions, lest it gets confusing.

Be sure to include detailed information about what you perceive to be the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to solve arrayindexoutofbound exception