1

我有一个对象:

namespace Picture{
    class ref Picture{
        System::String^ path
        int picNumber;
    };
}

然后我尝试制作一个数组并填充我path的主要内容:

TrackPicOnSlide = 2;
array<Picture::Picture^>^ ArrayPics = gcnew array<Picture::Picture^>(TrackPicOnSlide);
ArrayPics[0]->path = "HI";

但它给了我一个运行时错误:

An unhandled exception of type 'System.NullReferenceException' occurred in PPTAuto.exe

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

我究竟做错了什么?

4

1 回答 1

4

您正在创建一个新数组,Picture但您没有用任何图片填充数组本身,因此当您尝试访问位置 0 处的元素时,您将得到 null 这就是您收到异常的原因,您应该执行以下操作:

TrackPicOnSlide = 2;
array<Picture::Picture^>^ ArrayPics = gcnew array<Picture::Picture^>(TrackPicOnSlide);
ArrayPics[0] = gcnew Picture;
ArrayPics[0]->path = "HI";
// etc..
于 2013-06-24T23:14:36.567 回答