Nullreferenceexception in Winforms code

Asked

Viewed 124 times

1

exceção NRE no Visual Studio

I believe it’s a bug not the code, but the compiler...

When I eat this area of code, the error always goes to the next variable, or method.

Behold: exceção NRE no Visual Studio

Part of the code:

                grpBoxMDeck[i] = new System.Windows.Forms.GroupBox()
                {
                    Location = new System.Drawing.Point(76, yG),
                    Size = new System.Drawing.Size(536, 340),
                    BackColor = System.Drawing.Color.Transparent,
                    ForeColor = corLetra,
                    Font = new System.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 ran normally, even that last line:

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

Now not even it runs. I tried to close and open Visual Studio and nothing!

  • Wow, the compiler’s been around for years, millions of people use it every day, and no one had ever taken this bug in it? How did you manage to catch this mistake? You studied a lot of programming to get it?

  • Where is the statement of pMelhoresDecks?

  • One of your controls isn’t initialized, they’re null. To know which one, press F9 (to put a breakpoint) on the control line and check each of them (stopping over them with the mouse, or clicking on them and pressing Shift+F9). This is not a bug in the compiler, it is in the same code.

  • Leticia, all controls are initialized, until the first exception appears in the declaration of the variable byte X1 = 3; .

  • I could solve it like this: picImagemMDecks = new System.Windows.Forms.Picturebox[deck. Length > 10 ? 10 : decks.Length][];

1 answer

3


Try to do this:

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

According to the documentation this is how you create a new object PictureBox.

I can’t guarantee that it will work because we don’t have all the code to know. But everything suggests that this is it.

In fact most people write like this:

picImagemMDecks[i] = new PictureBox();

I put in the Github for future reference.

And the variable pMelhoresDecks needs to be initialized with some value before using. If you didn’t do this, the value is null and nothing can be done with it but initialize it. So you have to do this before you use it. You usually initialize it next to her declaration. Find it and give it a new something there.

Some tips:

  • Commenting an error code does not make the error disappear, usually causes more errors and probably harder to fix.
  • Closing Visual Studio does not make the error magically disappear.
  • The "never" problem is from the compiler.
  • Read the documentation of what you’re using before you use it. If you don’t understand something, I see what you need to learn first.
  • You’re finishing a house that has no foundation, it’s all gonna fall apart. First understand how things work, what is each thing, one step at a time, goes in the simple, one concept after another, when master something there you pass to the next. If you keep playing codes you don’t understand you’re not programming and every day you’re getting worse.
  • picImagemMDecks is a two-dimensional array, and I prompt it like this: System.Windows.Forms.Picturebox[][] picImagemMDecks; Then I create 8 images within each vector...

  • Why are you making an exception in the byte variable instantiation? As described, the compiler is always throwing an exception on the next line of code. If I comment the row of the byte variable, it throws an exception in the for (byte j = 0;

  • Well, I’ll mark your answer as a solution. But, I managed to solve it like this: picImagemMDecks = new System.Windows.Forms.Picturebox[deck.Length > 10 ? 10 : decks.Length][]; .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.