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 & HelpPrograms › an array of arraylists
Page Index Toggle Pages: 1
an array of arraylists (Read 466 times)
an array of arraylists
Apr 7th, 2010, 8:24am
 
so i am trying to create an array of arraylists and i keep getting a null pointer exception when i try to access them.

Code:
ArrayList[] blocks = new ArrayList[maxCol];

for(int i = 0; i< maxCol;i++){
   for(int x = 0; x< maxRow;x++){
     blocks[i].add(new Block(width/2+i*25, height/2+x*25, blocksize, int(random(0,2)), true));
   }
 }


how can i access them if at all possible?
Re: an array of arraylists
Reply #1 - Apr 7th, 2010, 8:34am
 
you need to initialise each array list, not just the array of them.

for(int i = 0; i< maxCol;i++){
   blocks[i] = new ArrayList();
   for(int x = 0; x< maxRow;x++){
     blocks[i].add(new Block(width/2+i*25, height/2+x*25, blocksize, int(random(0,2)), true));
   }
 }

Page Index Toggle Pages: 1