2

I've consumed a web service with two classes "address" and "request." One of the properties of the request object is an array of address objects:

request _req = new request();
_req.addresses = // expecting address[]

I know I'm doing this wrong (as I keep getting exception errors) so I'm hoping someone can help me out. How do I create an array of address objects and set the "_req.addresses" value equal to that object (address[])? I get an "object reference not set to an instance..." error on the second line, when trying to set the city value equal to the string _q.LocationA.City... so these aren't working:

    address[] _address = new address[1];
    _address[0].city = _q.LocationA.City;
    _address[0].state = _q.LocationA.State;
    _address[0].street = _q.LocationA.Address;
    _address[0].zipCode = _q.LocationA.Zip;

    request _req = new request();
    _req.addresses = _address;

And I've tried this:

    address _address = new address();
    _address.city = _q.LocationA.City;
    _address.state = _q.LocationA.State;
    _address.street = _q.LocationA.Address;
    _address.zipCode = _q.LocationA.Zip;

    request _req = new request();
    _req.addresses[0] = _address;
3
  • For a new request(), is adresses == null ? Commented Nov 13, 2009 at 19:13
  • Yes, that's exactly what's happening. Commented Nov 13, 2009 at 19:23
  • What exact exception are you getting, and at which exact line of code? Commented Nov 13, 2009 at 19:27

5 Answers 5

9

Your classes need to be instantiated separately from your array. C# won't call your constructor automatically, so that's why you get a NullPointerException in the first set of code. The second code fails because you're giving it a single object, instead of an array.

You essentially need to combine the two:

address[] _address = new address[1];
_address[0] = new address();
_address[0].city = _q.LocationA.City;
_address[0].state = _q.LocationA.State;
_address[0].street = _q.LocationA.Address;
_address[0].zipCode = _q.LocationA.Zip;

request _req = new request();
_req.addresses = _address;
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that did it. I've never seen that (or tried doing this before) so I never would have tried that... thanks for helping me out.
2

In the first code block you are not creating a new address object in the first element of the array; thus the null reference exception when you attempt to set the city member. The fix for this is:

address[] _a = new address[1];
_a[0] = new address();
_a[0].city = ...

In the second code block you are not creating an array in the _req.addresses member. The fix for that is:

...
_req.addresses = new address[1];
_req.addresses[0] = _address;

Hope this helps!

Comments

1

Change:

address[] _address = new address[1];
    _address[0].city = _q.LocationA.City;
    _address[0].state = _q.LocationA.State;
    _address[0].street = _q.LocationA.Address;
    _address[0].zipCode = _q.LocationA.Zip;

To:

address[] _address = new address[1];
_address[0] = new address();
_address[0].city = _q.LocationA.City;
_address[0].state = _q.LocationA.State;
_address[0].street = _q.LocationA.Address;
_address[0].zipCode = _q.LocationA.Zip;

Comments

0

I would advise you to use a generic list:

List<address> _addresses = new List<address>();
_addresses.Add(_address);

request _req = new request();    
_req.addresses = _addresses;

3 Comments

I tried this originally, but the _req.addresses property was giving an error because it was looking for "address[]" not List<address>... unless I missed something.
Check your Request class definition, how is the field addresses defined?
It's defined as "address[]" which is why I think "List<address>" isn't working.
0

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication1 { public class Address { public string Street; public string City; public string State; public string ZipCode; }

public class Request
{
   public Address[] address;
}


class Program
{
    static void Main(string[] args)
    {

        Address[] address = new Address[]{ new Address {
                    Street = "140 sw 8 st",
                    City = "Miami",
                    State = "Florida",
                    ZipCode = "33122"}};

        Request req = new Request();

        req.address = address;

    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.