java - How to create and use a two dimensional array of classes -
i experimenting 2 dimensional array of classes in java. wrote following (wrong) code:
public class gameevent { static int launch_alien = 0; static int end = 1; long time; long cum_time; int alien_type; int event_code; } then
gameevent[][] event_array = new gameevent[max_levels][max_events]; and accessed code below (read comment in code):
event_array[lev][ctr].event_code = code; // nullpointer exception @ runtime so question is, least painful way change code works.
gameevent[][] event_array = new gameevent[max_levels][max_events]; this allocates array of references gameevent objects not objects themselves.
to allocate obects necessary every cell of array:
for (int = 0; < max_levels; ++i) (int j = 0; j < max_events; ++j) event_array[i][j] = new gameevent();
Comments
Post a Comment