-4

I'm creating a android application. When I tested in Emulator, Samsung S8 Plus and General Mobile GM8, I haven't any error but play console always gives me some crushes.

Code is:

int count = getItemCount() >= 30 ? 30 : getItemCount();
if (count == 0)
    return new long[] {0};

long[] result = new long[count];
for (int i = 0; i < count; i++)
    result[i] = users.get(i).getPk();
return result;

Error message: ArrayIndexOutOfBoundsException

How I can fix it I really do not know.

zhh
  • 2,346
  • 1
  • 11
  • 22
  • 1
    for eg. if count is 4 and users list have only 3 data or less than count then it will throw `ArrayIndexOutOfBoundsException` – Nirav Bhavsar Aug 11 '18 at 09:11
  • 1
    maybe your users variable don't have any element – masoud vali Aug 11 '18 at 09:11
  • 1
    @Nirav Bhavsar What can I do then in this code? – Burcu Tarbil Aug 11 '18 at 09:24
  • @Burcu Tarbil, please refer my below answer – Nirav Bhavsar Aug 11 '18 at 09:33
  • Calling `getItemCount()` twice is dangerous (cuz value might vary depending on timing), you need to put it in a variable instead. I am guessing your error is a result of race condition (though not enough code to prove it). – Enzokie Aug 11 '18 at 09:43
  • I would suggest that you include the whole adapter code. Q: Are you using another thread for updating your adapter by chance? – Enzokie Aug 11 '18 at 09:56
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – dur Aug 11 '18 at 11:30

5 Answers5

1

remove the "=" sign in the condition it will work properly :

int count = getItemCount() > 30 ? 30 : getItemCount();
        if (count == 0)
            return new long[] {0};

        long[] result = new long[count];
        for (int i = 0; i < count; i++)
            result[i] = users.get(i).getPk();
        return result;
amit
  • 659
  • 1
  • 8
  • 21
  • How does this resolve OP's problem if the users list has fewer than 30 items? –  Aug 11 '18 at 09:37
  • can you give a full code with method getItemCount() and which line gives the error – amit Aug 11 '18 at 09:37
0

For eg. if count is 4 and users list have only 3 data or less than count then it will throw ArrayIndexOutOfBoundsException

To avoid ArrayIndexOutOfBoundsException try below approach

int count = getItemCount() >= 30 ? 30 : getItemCount();
    if (count == 0)
        return new long[] {0};

    long[] result = new long[count];
    if(users != null && users.size() > 0){
        for (int i = 0; i < count; i++){
            if(i >= users.size()){
                break;
            }
            result[i] = users.get(i).getPk();
        }
    }

return result;
Nirav Bhavsar
  • 2,133
  • 2
  • 20
  • 24
0

If what you want is an array with size equal to the size of the users list with maximum 30 items and minimum 1 element, then change count to the users list size if this size is less than 30:

    int count = getItemCount() >= 30 ? 30 : getItemCount();
    if (count > users.size())
        count = users.size();
    if (count == 0)
        return new long[] {0};

    long[] result = new long[count];
    for (int i = 0; i < count; i++)
        result[i] = users.get(i).getPk();
    return result;
0

It can happen because of out of index. Let me make it simple if array have 4 indexes so

a=[0,1,2,3]//eg:

SO that if you want to print a[0] then o/p=

0

But for a[5]: you will have ArrayOutofBoundsExeption

Here in your example you have not specified your user field How it is generated so after making some assumptions.. Im telling you

result[i] = users.get(i).getPk();

here the problem is occured in users.get(i) where user field is not that contains value but it may be dynamic: so my solution will be You should use Try exception and just pass it ArrayIndexOutOfBoundsException

and raise exception your program should work fine! I hope it will help you!

  • **It is bad idea to catch an unchecked exception like ArrayIndexOutOfBoundsException**, you should fix the code by finding the root issue rather than hiding it. – Enzokie Aug 11 '18 at 10:12
0

It seems the size of users list does not equal to counts and it is smaller. so to prevent ArrayIndexOutOfBoundsException, I suggest to edit your code in this way:

int count = getItemCount() >= 30 ? 30 : getItemCount();
count = count > users.size()  ? users.size() : count;
        if (count == 0)
            return new long[] {0};

        long[] result = new long[count];
        for (int i = 0; i < count; i++)
            result[i] = users.get(i).getPk();
        return result;

by modifying your code like that, in situations that users list is greater than counts, you will not have any controls on the items at the end of users list.

helenDeveloper
  • 624
  • 3
  • 8
  • 15