Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

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

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() + "
";

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...