We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › same object concurrently in multiple arraylists
Page Index Toggle Pages: 1
same object concurrently in multiple arraylists? (Read 556 times)
same object concurrently in multiple arraylists?
Jan 30th, 2009, 9:25pm
 
can the same object be in different arraylists at the same time? I am getting a nullpointerexception trying to add an object to different arraylists - is that a problem?

thanks
Re: same object concurrently in multiple arraylist
Reply #1 - Jan 30th, 2009, 9:33pm
 
No, not a problem at all. You don't put objects but references to them.
Re: same object concurrently in multiple arraylist
Reply #2 - Jan 30th, 2009, 9:42pm
 
that's what i thought. thanks so much for the reply...
Re: same object concurrently in multiple arraylist
Reply #3 - Jan 30th, 2009, 11:31pm
 
might be asking too much, but here's the applet:

http://www.upsdn.ca/processing/animal_particles_06/applet/

can't figure out what's wrong. And just my luck, the applet doesnt load properly in my browser...but there's access to the code.

thanks!
Re: same object concurrently in multiple arraylist
Reply #4 - Jan 31st, 2009, 9:55am
 
Ah, I see.
The line:
ArrayList wetAnimals, dryAnimals, mixedAnimals, hotAnimals, coldAnimals, temperateAnimals = new ArrayList();
doesn't work like you expect.
Only temperateAnimals with be initialized, the other lists will be null.
Write:
ArrayList wetAnimals = new ArrayList();
ArrayList dryAnimals = new ArrayList();
and so on.
Or:
ArrayList wetAnimals, dryAnimals, mixedAnimals, hotAnimals, coldAnimals, temperateAnimals;
wetAnimals = new ArrayList();
dryAnimals = new ArrayList();
Re: same object concurrently in multiple arraylist
Reply #5 - Feb 1st, 2009, 4:56am
 
thanks pHihLO! that's something that I never would have guessed. is there a clue that would have led me to intuiting that?

thanks again for all your help...i would be pretty lost without it
Re: same object concurrently in multiple arraylist
Reply #6 - Feb 1st, 2009, 10:05am
 
No, sometime I would be tempted to use the same shortcut... Smiley

For your information, you can use a similar shortcut:

int x, y, z;
x = y = z = 1.0;


It must be done in two steps. And that's not good for your case, because the array lists would be the same (ie. you assign the references to the newly created object, not a new object to each).
Page Index Toggle Pages: 1