0

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 {

ArrayList<String> random;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
        for (int a=0; a<11; a++){
        random.add("a");
    }
            }
    }

I know the above code does useless action but that is simplified from my problem in the for loop code in JAVA. And I got this error from the error log, "unhandled event loop exception". Can anyone point out that what I am doing wrong please?

2
  • 1
    Don't make me mock you :) That's not a question for SO! Commented Dec 10, 2011 at 22:50
  • @IvanNikolchov sorry. I am only a beginner. Commented Dec 11, 2011 at 0:26

2 Answers 2

2

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");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Also, perhaps add an integer with random.add(a) was intended, and not adding a String with random.add("a")
@JustinMuller: Possibly (or rather, random.add(String.valueOf(a));. It's hard to tell, with no description about the intended behaviour...
@JonSkeet Thanks, and the mistake was made when I edited the code on this website while I was trying to simplify the code.
0
for (int a=0; a<11; a++) /*Delete the semicolon here*/
{
    random.add("a");
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.