1

I want to create an array containing all the Pushpin objects I am dealing with. While trying to populate the Array, I am getting a NullReferenceException Unhandled error thrown. I have read as much documentation as I can find and cannot work out what is going on.

I have tried at least the following:

Pushpin[] arrayPushpins;
int i = 0;
foreach (Result result in arrayResults)
                {
                    Pushpin pin;
                    pin = new Pushpin();
                    pin.Location = d;
                    myMap.Children.Add(pin);
                    arrayPushpins[i] = new Pushpin();
                    arrayPushpins.SetValue(pin, i);;
                    i++;
                }

AND...

Pushpin[] arrayPushpins;
int i = 0;
foreach (Result result in arrayResults)
                {
                    Pushpin pin;
                    pin = new Pushpin();
                    pin.Location = d;
                    myMap.Children.Add(pin);
                    arrayPushpins[i] = new Pushpin();
                    arrayPushpins[i] = pin;
                    i++;
                 }

And nothing seems to work... I get the NullReference error every time. Any ideas? Many thanks! Will.

4 Answers 4

6

The problem is that you don't initialize your array:

Pushpin[] arrayPushpins = new Pushpin[10]; // Creates array with 10 items

You might consider using IEnumerable<Pushpin> if you don't know the number of items in advance, e.g:

IEnumerable<Pushpin> pushpins = new List<Pushpin>
Sign up to request clarification or add additional context in comments.

2 Comments

Why not use a IEnumerable Collection instead?
initialising the array fixed. Strange, I thought I had tried that too, but potentially not in combination with using the SetValue method. Thanks, Amittai. I will also try using a Collection, as I think you are all right - it will be better in this case as the number of pins can vary.
1

You didn't initialize the array

 Pushpin[] arrayPushpins = new Pushpin[/*number goes here*/];
    int i = 0;
    foreach (Result result in arrayResults)
                    {
                        Pushpin pin;
                        pin = new Pushpin();
                        pin.Location = d;
                        myMap.Children.Add(pin);
                        arrayPushpins[i] = new Pushpin();
                        arrayPushpins.SetValue(pin, i);;
                        i++;
                    }

Edited to add: I'd avoid using a raw array, and go with something like a List<Pushpin> instead

Comments

1

I think you should use a list instead of an array. That way, you won't have to know in advance how many elements will you have in your list.

Comments

0

In your code, the array is only declared, not initialized. You need to initialize it with the new keyword.

Pushpin [] arrayPushpins= new Pushpin[50]; 

As the other answers recommend, you can use lists or collections.

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.