Skip to content Skip to sidebar Skip to footer

Can't Add Element To Arraylist In For Loop

I am currently having the problem of not able to run the following code in Android app development. import java.util.ArrayList; public class Test extends FragmentActivity { Array

Solution 1:

There are at least two problems (I suspect).

First, you're getting a NullPointerException because you're not initializing random with a value referring to an actual object.

Next, your syntax is bad here:

for (int a=0; a<11; a++);

Your code is only adding a single element to random - it's equivalent to:

for (int a=0; a<11; a++)
{
}
random.add("a");

I very much doubt that that's what you were intending. My guess is that you wanted this instead:

for (int a=0; a<11; a++)
{
    random.add("a");
}

Solution 2:

for (int a=0; a<11; a++) /*Delete the semicolon here*/
{
    random.add("a");
}

And also you need to initializing the ArrayList "random"。

Post a Comment for "Can't Add Element To Arraylist In For Loop"