Search code examples
c#.netxamlmaui

Why is my array in my xaml data binding empty and saying its not an instance of an object?


In my mixingPage xaml file I am trying to set the text content of a label to a value. I have a song which has an array of effects (abstract class) and an effect which has an array of effectItems. The problem occurs when i try to get data from the array in the effectItem. retrieving data from the effect array works fine. It does not matter which data I try to get from the effectItem array nor does it matter if the effect is abstract or not.

The mixingPage xaml code:

<Label Grid.Column="0"
Text="{Binding Effects[0].EffectItems.Length}"
VerticalOptions="Center"
Margin="10,0" />

The code navigating to this page:

Song song = new Song
{
    Effects = new List<Effect>
    {
        new Equalizer
        {
            EffectItems = new[]
            {
                new EffectItem { Name = "Low", Min = 1, Max = 0, Value = 5 }
            }
        }
    },
};

var mixingPage = new MixingPage();
mixingPage.BindingContext = song;
Navigation.PushAsync(mixingPage);

But it always gives the error:

0>MixingPage.xaml: Error  XamlC: Object reference not set to an instance of an object.

I am using .NET Maui 6.0

I was expecting to write something along the lines of "{Binding Effects[0].EffectItems[0].Value}" and return the value stored in the effect item.

I tried removing the "abstract" from the class and using it as a alone standing class, I tried initializing the classes in the navigation in all different ways, changing the arrays to lists. It always gives the error no matter what.

Edit I forgot to specify that the Equalizer class you see in the song is a class extending the Effect abstract class.


Solution

  • So I guess I found a way around it. I changed the Effect class to just have a field with a "category" string instead of an effectItem which belongs to an effect. Not the way I would have liked it to work, but I have been at it for about a week now with no results.