Saturday, December 8, 2012

lovely warning message : "Object reference not set to an instance of an object." while trying to send a request to an end point

When  I try to create a request by a webservice and  get a response out of it,  I get this lovely warning message : "Object reference not set to an instance of an object.", I always see an animation of "little hearts" flying around me. I feel luv, I feel happiness... Of course these words are irrelevant  to explain my anger towards this snappish warning that I face with in my daily programming life .o_O.

The reason for this error is that you are trying to create a whole car without creating first its components such as wheel, glasss, engine and etc.   Ok, let me put this kind of examples away and give a nice example for hard-core coders [as if a hard-coder gets such an error :-) ].

Here how it is,  you have a webservice  "StudentComponent" that has SearchStudentReqıest service that lets you enter some parameters such as name, surname, courses[].  Now when you create a request object in your .cs file like given below;

Student student = new Student(); = "Mike";
student.surname = "Givemefive";[5] = new stı;
stı[0] = "Math";       *[1]= 'Scinece";    *

Here the weird thing shows up if you implement your functionality as given above.   The lines marked with "*" cause this problem.  While dealing with SOA,  I am not sure why, you have to initialize each object of the array like[0] = new StudentComponent.tcourse();
student.course[1] = new StudentComponent.tcourse();

After doing this, you will have no problem at all.

Note: tcourses() function is assumed to be given in the webservice.

If you can explain it better with your experience , please be my guest and leave a comment ;)

No comments:

Post a Comment