Question

I'm trying to consume a DLL-located method in C#, which returns a dynamic array of structs. What ever I do, I receive the well-know "Object reference not set to an instance of an object" error, Here is my last code and it still tells that error:

string v_user = "kish";

        string v_pass = "u";

        string v_number = "p";

        string v_address = "url has been replaced with this string";

        string v_cid = "abc";

        Cls_SMS.SMSReceive.STC_SMSReceive[] xts;

        Cls_SMS.SMSReceive px = new Cls_SMS.SMSReceive();

   // *** is the below line

        xts = px.ExtendReceiveSMS(v_user, v_pass, v_number, v_address, v_cid);

        int upper_bound = xts.GetUpperBound(0);

        for (int counter = 0; counter < upper_bound; counter++)

        {

            Response.Write(xts[counter].Message.ToString());

            Response.Write("<br>");

        }

please note that my main problem is about receiving a dynamic array of structs with struct type name (Cls_SMS.SMSReceive.STC_SMSReceive) and other aspects such as connecting to the remote server is not my problem. I just want to allocate a dynamic array of vendor-defined structs to the left side of the assignment opeator in * line.

Please help me. Thank you very much.

Was it helpful?

Solution

It is not clear how the px.ExtendReceiveSMS(v_user, v_pass, v_number, v_address, v_cid); method assigns the array, it probably doesn't assign it at all because of the exception. Here's how you could assign a dynamic array and return it:

public STC_SMSReceive[] ExtendReceiveSMS()
{
    STC_SMSReceive[] result = new STC_SMSReceive[2];
    result[0] = new STC_SMSReceive();
    result[1] = new STC_SMSReceive();
    return result;
}

Also if it is dynamic you might also take a look at List<T>:

public IList<STC_SMSReceive> ExtendReceiveSMS()
{
    IList<STC_SMSReceive> result = new List<STC_SMSReceive>();
    list.Add(new STC_SMSReceive());
    list.Add(new STC_SMSReceive());
    return result;
}

OTHER TIPS

This has nothing to do with the strict array; simply, the library method you are using is returning null.

There are various possibilities here:

  • maybe returning null is an expected return value for some scenarios; check the documentation
  • maybe you need some additional configuration, or maybe you need to call some additional method (GetTheData() would be too hopeful ;p), or wait for some other event before this data is available - check the documentation
  • maybe it is simply a library bug; contact the vendor

If all 3 routes fail, personally I'd just open it reflector and look for a scenario that might return null. Then tell the vendor to fix the bug or clarify the documentation as appropriate.

If you replace your separate declaration of xts with:

var xts = px.ExtendReceiveSMS(v_user, v_pass, v_number, v_address, v_cid);

what type does Visual Studio now report xts to be?

You can tell by hovering over xts with your cursor and reading it off the tooltip.

Other than that if the vendor is reporting that it works for other users, you must have one (or more) of the arguments wrong. Ask the vendor for some example code that works so you can check to see if that connects to the server properly. If it does then the error is in the other arguments, if not then it's a problem with your connection to the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top