• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Populating ArrayList with Objects

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh my god... *facedesk* Thanks for help everyone. Guess I need to pay more attention to compiler messages.
 
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic