【问题标题】:An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dllSystem.Windows.Forms.dll 中出现“System.NullReferenceException”类型的未处理异常
【发布时间】:2015-05-18 18:02:44
【问题描述】:

好吧,我正在编写一个 Yachzee 游戏,但是它运行得不是特别好。

当我单击“滚动”按钮时,此代码将启动。

         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);

它创建五个单独的随机数并将它们作为五个单独的对象发送到我的 Dice.h。

这是 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;
}

};

就像现在一样,程序以一个指向行的箭头中断

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

如果我移动显示的行

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

在 else 下,改变 SizeMode 的行将中断,箭头指向 if、else if 或 else,它们的数字对应于 rand 的值。如果我在下面看它似乎显示变量的所有不同值的地方,它将显示这个

Name    |   Value                                       |   Type
_________________________________________________________________

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

最后要补充的是,它在中断弹出窗口中显示以下内容

附加信息:对象引用未设置为对象的实例。

【问题讨论】:

  • 你没有分配PictureBox。它只是一个null 变量。您不能在null 上设置Size,它只是不起作用。您需要先创建PictureBox 的实例。 PictureBox^ p = new PictureBox(); 或类似的。 (我不记得确切的语法了。)
  • 我在这里看不到 c# 代码和 C++ 代码,只有带有 Windows 窗体的 C++/CLI。我已经更新了标签来替换它。另外,如果您打算使用 C++/CLI,我建议您使用 .Net Random 而不是 C 库 rand()
  • @EBrown - 这是在我注释掉的那一行完成的吗? (createPictureBox函数中的第一个)如果没有,那是怎么做的?
  • @Trisstar 是的。这就是应该做的地方。

标签: winforms visual-studio-2012 c++-cli


【解决方案1】:

您需要创建 PictureBox 控件的实例并将其作为子控件添加到窗体或容器控件中。

【讨论】:

  • 这是在我注释掉的那一行完成的吗? (createPictureBox函数中的第一个)如果没有,那是怎么做的?
  • 部分是的......不要忘记将控件添加到表单层次结构中。
  • 另一种解决方案是重用窗体上已有的现有图片框控件。固定数量的骰子可能对重用现有控件更有意义。
【解决方案2】:

运行程序时,异常设置窗格将在 vs 的底部可见。打开它,然后在搜索框中键入 NullReferenceException 并选中 System.NullReferenceException 框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多