I have a string array
public: array<String ^> ^ sss;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
array<String ^> ^ sss = gcnew array<String ^>(3);
sss[0]="asdasd";
sss[1]="s115ss";
sss[2]="s115ss";
}
I need to show the 1st element into a textbox.
I used
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
textBox2->Text = sss[0];
}
Vc++ gave System.NullReferenceException. Why? And how to fix it?
The error:
An unhandled exception of type ‘System.NullReferenceException’ occurred in test000.exe
Additional information: Object reference not set to an instance of an object.
Your code shouldn’t compile, unless you also have a field called
sss. If that’s the case, you want to set the value of that field in your constructor, not of some unrelated local variable with the same name: