0

I am adding a value to my arraylist but it is showing error. Before adding the value I am displaying it first. The value is displaying but it is not adding to the arraylist. Please help me regarding this...

My Code:

static ArrayList<String> allfirstids;

ArrayList<String> list = List.get(i);
UserBO user = new UserBO();

user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));

allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);

Thanks in advance...

1
  • it is showing error? what error? Commented Apr 23, 2012 at 4:58

2 Answers 2

5

before adding, please initialize arraylist.

 allfirstids = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

4 Comments

Hi I made very silly mistake by not initializing arraylist....thank you..now its working....
Hi everytime when I call the page the list.get(0) contains different value. For suppose first time list.get(0)=10 then first 10 is added...again second time in list.get(0) = 20 then it is replacing the 10...But I want to add 10,20,...to my arraylist. How can I do that?
dont initialize allfirstids inside loop. it will reinitialize always and add the recent data. If you can post the complete method, i can tell you what exactly you have to do
if it is inside a method, you can also do like this. put condition while initializing arraylist if(allfirstids == null) { allfirstids = new ArrayList<String>();}
1

You forgot to initialize your static ArrayList allfirstids, Just initialize it, before using..

static ArrayList<String> allfirstids = new ArrayList<String>();

ArrayList<String> list = List.get(i);
UserBO user = new UserBO();

user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));

allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);

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.