-1

Okay so, I'm programming a Yachzee game, and well, it's not working perticulary well.

When I click the button "Roll" this code is started.

         int rand1 = rand()%6+1;
         int rand2 = rand()%6+1;
         int rand3 = rand()%6+1;
         int rand4 = rand()%6+1;
         int rand5 = rand()%6+1;

         Dice^ t1 = gcnew Dice (rand1);
         Dice^ t2 = gcnew Dice (rand2);
         Dice^ t3 = gcnew Dice (rand3);
         Dice^ t4 = gcnew Dice (rand4);
         Dice^ t5 = gcnew Dice (rand5);

It creates five seperate random numbers and send them to my Dice.h as five seperate objects.

This is the code in Dice.h

using namespace System::Windows::Forms;

ref class Dice {

public:
    Dice (int rand)
    {
        this->rand = rand;
        createPictureBox();
    }

private:
    int rand;
    PictureBox^ p;

public:
void createPictureBox()
{
        //PictureBox^ p = gcnew PictureBox();

        p->Size = System::Drawing::Size(91, 85);
        if ( rand == 1 )
            p->ImageLocation = "..\\Bilder\\dice_face_1.png";
        else if ( rand == 2 )
            p->ImageLocation = "..\\Bilder\\dice_face_2.png";
        else if ( rand == 3 )
            p->ImageLocation = "..\\Bilder\\dice_face_3.png";
        else if ( rand == 4 )
            p->ImageLocation = "..\\Bilder\\dice_face_4.png";
        else if ( rand == 5 )
            p->ImageLocation = "..\\Bilder\\dice_face_5.png";
        else
            p->ImageLocation = "..\\Bilder\\dice_face_6.png";
        p->SizeMode = PictureBoxSizeMode::StretchImage;
}

public:
PictureBox^ getPictureBox()
{
    return p;
}

int getRand()
{
    return rand;
}

};

As it is now, the program breaks with an arrow pointing to the row which says

p->ImageLocation = "..\\Bilder\\dice_face_1.png";

And if I move the row which says

p->Size = System::Drawing::Size(91, 85);

under the else, where the row changing the SizeMode is it will break with a arrow pointing to the if, else if, or else which have the number corresponding to the value of rand. And if I look below where it seems to show all the different values of variables it will show this

Name    |   Value                                       |   Type
_________________________________________________________________

this    |   0x02b6e9a4 { rand=1 p=<undefined value> }   |   Dice^

Last thing to add is that it says the following in the break pop up

Additional information: Object reference not set to an instance of an object.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • 3
    You don't allocate the `PictureBox`. It's just a `null` variable. You can't set the `Size` on a `null`, it just *won't work.* You need to create an instance of the `PictureBox` first. `PictureBox^ p = new PictureBox();` or similar. (I can't remember the exact syntax.) – Der Kommissar May 18 '15 at 18:03
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Charles Mager May 18 '15 at 18:07
  • I see no c# code nor C++ code here, only C++/CLI with Windows Forms. I have updated the tags to replace this. Also, if you are going to be using C++/CLI, I would suggest that you use [.Net Random](https://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx) instead of the C library `rand()`. – crashmstr May 18 '15 at 18:07
  • @EBrown - Is that something that is done on the line I commented away? (The first one in the createPictureBox function) If not, how is that done? – PatchingMatching May 18 '15 at 18:19
  • @Trisstar Yes. That is where it should be done. – Der Kommissar May 18 '15 at 18:20

2 Answers2

0

You need to create an instance of the PictureBox control and add it as a child to the form or a container control.

Marc Johnston
  • 1,276
  • 1
  • 7
  • 16
  • Is that something that is done on the line I commented away? (The first one in the createPictureBox function) If not, how is that done? – PatchingMatching May 18 '15 at 18:17
  • Partially yes ... don't forget the adding of the control to the form hierarchy. – Marc Johnston May 18 '15 at 18:25
  • An alternate solution is to reuse an existing picture box control already on the form. A fixed number of dice probably makes more sense to reuse existing controls. – Marc Johnston May 18 '15 at 18:26
0

While running the program, Exception Settings pane will be visible at the bottom of the vs. Open it then type NullReferenceException in the search box and check the System.NullReferenceException box.

DiRiNoiD
  • 1,281
  • 2
  • 18
  • 24