NullReferenceException in Winforms code

1

Ithinkit'sabugnotinthecode,butinthecompiler...

WhenIeatthiscodearea,theerroralwaysgoestothenextvariable,ormethod.

See:

Partofthecode:

grpBoxMDeck[i]=newSystem.Windows.Forms.GroupBox(){Location=newSystem.Drawing.Point(76,yG),Size=newSystem.Drawing.Size(536,340),BackColor=System.Drawing.Color.Transparent,ForeColor=corLetra,Font=newSystem.Drawing.Font(Font.FontFamily,8.25f,System.Drawing.FontStyle.Bold),Text=string.Format("Elixir Médio: {0:f1} - Arena {1}+", elixirMedio, decks[i].Split('|')[1]).Replace(',', '.')
                };
                grpBoxMDeck[i].Click += (s, e) => pMelhoresDecks.Select();
                grpBoxMDeck[i].ContextMenuStrip = cmsGBMDecks[i];

                picImagemMDecks[i] = new System.Windows.Forms.PictureBox[8];
                byte x1 = 3, x2 = 3;

                for (byte j = 0; j < picImagemMDecks.Length; j++)
                {
                    byte copiaJ = j;
                    picImagemMDecks[i][j] = new System.Windows.Forms.PictureBox()
                    {
                        Size = new System.Drawing.Size(131, 157),
                        SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage,
                        BackColor = System.Drawing.Color.Transparent
                    };
                    if (j < 4) { picImagemMDecks[i][j].Location = new System.Drawing.Point(x1, 15); x1 += 133; }
                    else { picImagemMDecks[i][j].Location = new System.Drawing.Point(x2, 174); x2 += 133; }
                    grpBoxMDeck[i].Controls.Add(picImagemMDecks[i][j]);
                }

                pMelhoresDecks.Controls.Add(grpBoxMDeck[i]);

Any solution to this bug ? It appeared out of nowhere, the code was running normally, even this last line:

pMelhoresDecks.Controls.Add(grpBoxMDeck[i]);

Now it does not even run. I've tried closing and opening Visual Studio and nothing!

    
asked by anonymous 29.04.2018 / 22:35

1 answer

2

Try doing this:

picImagemMDecks[i] = new System.Windows.Forms.PictureBox();

According to documentation is so you create a new object PictureBox .

I can not guarantee that it will work because we do not have all the code to know. But everything leads one to believe that it is.

In fact most people write like this:

picImagemMDecks[i] = new PictureBox();

And the variable pMelhoresDecks needs to be initialized with some value before using. If you did not do this the value of it is null and nothing can be done with it except initialize it. So you have to do this before using. Normally the initialization is done next to the declaration of it. Find it and give new something there.

Some tips:

  • Commenting a code that gives error does not make the error disappear, usually causes more errors and probably more difficult to solve.
  • Closing Visual Studio does not make the error magically disappear.
  • The problem "never" is the compiler.
  • Read the documentation of what you are using before using. If you do not understand something, I see what you need to learn first.
  • You are finishing a house that has no foundation, everything will collapse. First understand how things work, what each thing is, one step at a time, going in the simple, one concept after another, when you master something there you move on to the next. If you keep playing codes you do not understand, you're not programming, and every day it gets worse.
29.04.2018 / 22:55