Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

IndexOutOfBoundsException when adding new item on ViewHolder

Hi everyone!! I have a quiestion, sorry for my ignorance. I am trying to add an item to a listView when the user clicks a button, but since the viewHolder has been created by that point it throws me an IndexOutOfBoundsException, I know that is because when adding the item I am making the List of Strins bigger, how can I fix that? Please help guys!!

Here´s the error = java.lang.IndexOutOfBoundsException: Invalid index 15, size is 15

and here is the piece of code where I try to add the item =

userMessage = userTextMessage.getText().toString(); listMessages.add(userMessage); chatAdapter.notifyDataSetChanged();

2 Answers

Hey,

I have no idea what listMessage is whether it's an array or what type of array, so I could be way off base but I also may be dead on lol. If you are already using an arrayList for listMessages, you can skip to the bottom lol.

So first things first. I made a mistake in my syntax, I made a mixture of Python and Java lol. I switch between the two so often it's confusing lol. So let's first make what I called String X an array so it works with java.

String[] X = { "My", "name", "is" };

Ok, now you want to add an item to an array. OOOOO (pause) dramatic effect.

The size of an array can't be modified. Once the array is created, that's it. You are done. But wait? That's crazy right? Why can't I add to an array after it's built? Well, I assure you a good answer is a topic for another day lol. The take home is that an array is fixed in size and can't be added to. BUT I wouldn't leave you hanging would I?

Nope! There is a solution and it's called an ArrayList. An ArrayList has built in methods to handle adding and removing items. Let me right an example.

In the below, I create a List and the <String> is called parameterizing. This says that I have a List that will only take Strings objects. Since it's still an array and an array is a collection of objects. I need to define the type of objects that will be in my list. I do that with <String>. Than I say new ArrayList meaning instantiating an ArrayList object to hold my string objects, The ArrayList has the methods for add remove etc. I have to declare the objects it holds on both sides of the equals. So that is why I say <String> again.

List<String> X= new ArrayList<String>(); 

Now there we go. We get a list on the (your left) and an ArrayList or implementation on the (your right). This comes from java collections and is talked about in the java treehouse course. So I definitely suggest taking that. Since they cover this topic very well. Ok, now moving on.

In the java repl

String[] X = { "My", "name", "is" }; <------ Create an empty list.
java> X
java.util.List<java.lang.String> X = []; <---------Here is my empty list 
java> X.add("Who");                         <--------------Let's add a string element to it
java.lang.Boolean res2 = true
java> X.add("Are");                             <--------------Let's add a string element to it
java.lang.Boolean res3 = true
java> X.add("you");                              <--------------Let's add a string element to it
java.lang.Boolean res4 = true
java> X
java.util.List<java.lang.String> X = [Who, Are, you]    <--------------SWEET new array has been created with my elements.

I believe this is what you are looking for. An ArrayList?

listMessages might already be an arrayList. If this is the case than you can add to it with the add method. If that is the case, and you are accessing an element outside the scope of the array. I need to know how you are accessing the element. I have seen people get the length of an array, than add 1 and get this error. Because they got the length, added something to the arrayList and then added 1 to the length since they got the length before they added the item. This isn't a good idea but the point is, I need to know how you are accessing the arrayList.

The error is saying you are accessing an element that's not there. So there could be a timing issue from when you add this item and then try to get this item.

So is the first part right and you need an array list or is the latter right and you are still confused? Let me know and if you are still confused and let me know how you are accessing data etc.

WOW!! now I understad!!! yes I want to use an Array, let's see if I can do it now ♥♥♥ Thanks a lot Ryan!!

Hey Carla,

No ignorance here. At least I mean I had to read it twice to get what you were asking so don't blame yourself. In fact never do that lol.

It's hard to determine your issue from what you posted.

userMessage = userTextMessage.getText().toString(); listMessages.add(userMessage); chatAdapter.notifyDataSetChanged();

You are saving userTextMessage.getText().toString.. Ok cool So you take a bunch of text. So say for example. If I were to print it.

System.blah.blah.print(userMessage);

OUTPUT
"This is a string of text form some kind of text box or whatever"

Ok fine, what you do next is say

listMessage.add(userMessage);

You are adding some kind of text to what I assume is a list or some type of object that has an add method. Ok cool.

Then you say

cahtAdapter.notifyDataSetChanged()l

So you are notifying the user that some data set has changed. Ok cool

The error is index out of bounds 15. Which means your are trying to access an element within a range that is not there. Take this example....

String X = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

Ok, so 1 is actually 0 and that means 15 is actually 14..

So this means 1 of two things. Either you are trying to access 15 and 15 isn't actually there despite there being 15 numbers. Or you are access element 15 before it's actually there.

Does this sound like a possible result??? If not please let me know. If you post more of the code I can send you in a better direction. But based off of what you posed this is my synopsis.

Hi Ryan!! Yes I actually think that I am trying to access the element number 15 when there´s only 14 elements stored, mmm but how can I add a new alement on String X when a user clicks a button? Thanks :D XD!!