vb.net - Trying to assign pictures to PictureBoxes in VB -


i trying create simple game, , first needs randomly load 16 pictureboxes images. not sure problem lies in this.

public class form1  private picarrows() picturebox = {pic11, pic12, pic13, pic14,                                      pic21, pic22, pic23, pic24,                                      pic31, pic32, pic33, pic34,                                      pic41, pic42, pic43, pic44}  private sub btnnew_click(sender object, e eventargs) handles btnnew.click     'starts new game      'declares rng      dim randgen new random      'uses rng determine arrow placement     intpicbox integer = 0 15         select case randgen.next(1, 5)             case 1                 picarrows(intpicbox).image = my.resources.up             case 2                 picarrows(intpicbox).image = my.resources.right             case 3                 picarrows(intpicbox).image = my.resources.down             case 4                 picarrows(intpicbox).image = my.resources.left         end select     next end sub end class 

i nullreferenceexception error on line after case x. know i'm doing wrong?

i nullreferenceexception error on line after case x

you cannot initialize array this:

public class form1      private picarrows() picturebox = {pic11, pic12, pic13, pic14,                                      pic21, pic22, pic23, pic24,                                      pic31, pic32, pic33, pic34,                                      pic41, pic42, pic43, pic44} 

the form has not been initialized yet, , controls on have not been created yet. result, control references going nothing, leaving array full of nothings. result nullreferenceexception because nothing not have image property.

you can declare array there, can initialize after form's constructor runs (sub new). form load place:

public class form1      private picarrows picturebox()                  ' best results should use same rng on , on too:         private randgen new random()          ...         private sub form_load(....              picarrows = new picturebox() {pic11, pic12, pic13, pic14,                                  pic21, pic22, pic23, pic24,                                  pic31, pic32, pic33, pic34,                                  pic41, pic42, pic43, pic44} 

see nullreference exception in visual basic


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -