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?
There are at least two problems (I suspect).
First, you’re getting a
NullPointerExceptionbecause you’re not initializingrandomwith a value referring to an actual object.Next, your syntax is bad here:
Your code is only adding a single element to
random– it’s equivalent to:I very much doubt that that’s what you were intending. My guess is that you wanted this instead: