Reading values into a List object - Object reference not set to an instance of an object

筅森魡賤 提交于 2019-12-13 04:29:54

问题


Trying to load values into a List object, I get the "Object reference not set to an instance of an object" error.

(edited question to simplify it)

MyVec.h listing (essentials only):

ref class MyVec
{
public:
    List<double>^ MyVector;

MyVec(void);
};

MyVec.cpp listing (essentials only):

#include "MyVec.h"

MyVec::MyVec(void)
{
}

Form.h listing (essentials only):

MyVec^ TestVec = gcnew MyVec();
double MyDouble = 1.002;
TestVec->MyVector->Add(MyDouble);
textBox1->Text = TestVec->MyVector[0].ToString() + "\r\n";

I get the error where I try to assign MyDouble to TestVec: TestVec->MyVector->Add(MyDouble)

In the Autos window it says (amongst other things) TestVec->MyVector undefined value

What's wrong?


回答1:


This change should solve the problem:

MyVec::MyVec(void)
{
    MyVector = gcnew List<double>();
}

Class member MyVector is initailly null reference. You need to initialize it before using, and MyVec constructor is appropriate place to do it.



来源:https://stackoverflow.com/questions/18164703/reading-values-into-a-list-object-object-reference-not-set-to-an-instance-of-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!