2

Here is my class pax

 public class pax
    {
        public pax();

        [SoapElement(DataType = "integer")]
        public string age { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string paxType { get; set; }
        public string title { get; set; }
    }

and i have declare the following array

pax[][]rooms=new pax[3][];

        rooms[0][0].paxType = "Adult";
        rooms[0][1].paxType="Adult";
        rooms[0][2].paxType="Child";
        rooms[0][2].age = "6";

Its throwing an error Object reference not set to an instance of an object. on line

 rooms[0][0].paxType = "Adult";

1 Answer 1

4

This will only give you array.

pax[][]rooms=new pax[3][];

To instantiate object, you have to new it:

rooms[0][0] = new pax();

You might be coming from C++ and may think that object array automatically create all objects, but that's not the case here - you will have to create each one because it is null before you do it.

EDIT:

Since you have jagged array here:

pax[][]rooms=new pax[3][];
rooms[0]=new pax[3];
rooms[0][0]=new pax();

Jagged array = array of arrays. If you need multidimensional (2-dimensional array), that's different story, and you would say:

pax[,] rooms=new pax[3,3];

for example...

Sign up to request clarification or add additional context in comments.

2 Comments

pax[][]rooms=new pax[3][]; rooms[0][0] = new pax(); rooms[0][0].paxType = "Adult"; but still the same error

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.