This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Populating ArrayList with Objects  RSS feed

 
Dem Snd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there is really annoying problem that i need to solve. Java won't let me create uninitialized objects to fill an arraylist.

what i want to do:

ArrayList<Object> list;

for(int i = 0; i < 10; i++){
list.add(new Object());
}

after this i would fill the objects with values when looping through the list again. Not sure if this is possible. What I need to do is generate an abstract number of distinct objects that can be assigned values by index.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dem Snd wrote:Java won't let me create uninitialized objects to fill an arraylist.

I suspect that's not your problem. The problem is probably that you haven't created the List which you are trying to add the objects to.
This line

just creates an ArrayList reference. You still need to create the actual Arraylist.
 
Daniel Georgescu
Greenhorn
Posts: 5
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The compiler complains that the ArrayList is not initialized, not the Objects used for filling it.
Compiler's message is : The local variable list may not have been initialized.
Your code should look like :
 
Dem Snd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh my god... *facedesk* Thanks for help everyone. Guess I need to pay more attention to compiler messages.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dem Snd wrote: . . . Guess I need to pay more attention to compiler messages.
What compiler message? Surely that code compiled all right and suffered a NullPointerException at runtime?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original code would not compile without an error, unless list was a member variable.

Only member variables are initialized to null if you don't initialize them explicitly. Local variables must be explicitly initialized, or you get an "variable may not have been initialized" error from the compiler.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean about implicit initialisation. But I presumed the blank line in the code posted hid the method heading.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!