Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
here if my codes:
C++
public:ref class d
		{
		public:
			int i;
		};
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 array<d^>^ u = gcnew array<d^>(4);
				 u[1]->i = 9;

			 }


then run the program,there comes out a NullReferenceException. I don't know why and how to fix it.
I want to use u[0],u[1],u[3]....
Posted

1 solution

Some small misktakes in the code.. I have corrected them.. You are creating a new array with 4 elements but each element also needs to be initialized before using... no need to put public:ref use public ref instead

C++
public ref class d 
	   {
	   public:
		   int i;
	   };


Here I have crated a new object d for u[1] before using it..

C++
     array<d^> ^u = gcnew array<d^>(4);
u[1] = gcnew d;
u[1]->i = 9;


Hope this helps...
 
Share this answer
 
Comments
XRay2 31-May-12 1:49am    
Oh!!!Thank you very much!!
Can I use List<d^> like that?
Jim Jos 31-May-12 2:05am    
Ya List<d> also you need to initialize every member!
XRay2 31-May-12 5:01am    
ok.thanks again~

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900