Object reference not set to an instance of an object

axbeitaxbeit Member ✭✭

Hi there. I receive an error (the error in the title) and I do not understand where this error is coming from.

<StackLayout Padding="20" Spacing="50">
        <Label x:Name="label"
               Text="Sample Text"
               FontSize="16"/>

        <Picker Title="Font Size">
            <Picker.Items>
                <x:String>Font Size = 8</x:String>
                <x:String>Font Size = 10</x:String>
                <x:String>Font Size = 12</x:String>
                <x:String>Font Size = 14</x:String>
                <x:String>Font Size = 16</x:String>
                <x:String>Font Size = 20</x:String>
                <x:String>Font Size = 24</x:String>
                <x:String>Font Size = 30</x:String>
            </Picker.Items>

            <Picker.SelectedIndex>
                <Binding Source="{x:Reference label}"
                         Path="FontSize">
                    <Binding.Converter>
                        <toolkit:ObjectToIndexConverter x:TypeArguments="x:Double">
                            <x:Double>8</x:Double>
                            <x:Double>10</x:Double>
                            <x:Double>12</x:Double>
                            <x:Double>14</x:Double>
                            <x:Double>16</x:Double>
                            <x:Double>20</x:Double>
                            <x:Double>24</x:Double>
                            <x:Double>30</x:Double>
                        </toolkit:ObjectToIndexConverter>
                    </Binding.Converter>
                </Binding>
            </Picker.SelectedIndex>
        </Picker>
    </StackLayout>
Tagged:

Answers

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    You need to look at the stack trace in the application output to see where the error is coming from.

  • axbeitaxbeit Member ✭✭

    @JamesLavery said:
    You need to look at the stack trace in the application output to see where the error is coming from.

    Hey there. I assume I can see the stack trace in the "Call Stack" Window?
    I've been looking into it and it is empty. To be sure that my call stack is not bugged I commented my part of my code out and the stack trace work fine. I even put a breakpoint way before my page is initialized and the error occurs before the initialisation.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Not the Call Stack - try the Application Output window. Let the app run to the end (i.e. actually crash), and you should see information on the exception and stack trace in the Application Output window.

  • axbeitaxbeit Member ✭✭

    @JamesLavery said:
    Not the Call Stack - try the Application Output window. Let the app run to the end (i.e. actually crash), and you should see information on the exception and stack trace in the Application Output window.

    There seems to be no output.
    Again tested it with code commented out where it works fine.
    But when the code is in it is completly empty.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Is the error occurring when running the app, or when building it?

  • axbeitaxbeit Member ✭✭

    @JamesLavery said:
    Is the error occurring when running the app, or when building it?

    while building it.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Ah - yes this is harder to diagnose.

    This is likely to be a problem with your XAML - either a page's XAML or your app.xaml.

    You won't get anything in the application output or the call stack. Don't forget the call stack is showing the stack when the app is running - the app hasn't managed to build, so won't be running! For the same reason the application output will be empty.

    Why do you think it's to do with the XAML you posted?

  • axbeitaxbeit Member ✭✭

    @JamesLavery said:
    Ah - yes this is harder to diagnose.

    This is likely to be a problem with your XAML - either a page's XAML or your app.xaml.

    You won't get anything in the application output or the call stack. Don't forget the call stack is showing the stack when the app is running - the app hasn't managed to build, so won't be running! For the same reason the application output will be empty.

    Why do you think it's to do with the XAML you posted?

    I see thanks.

    I think it has something to do with my XAML because if I comment that part out I can build that App.

    To be more precise I only have to comment out this part:

                            <x:Double>8</x:Double>
                            <x:Double>10</x:Double>
                            <x:Double>12</x:Double>
                            <x:Double>14</x:Double>
                            <x:Double>16</x:Double>
                            <x:Double>20</x:Double>
                            <x:Double>24</x:Double>
                            <x:Double>30</x:Double>
    

    for the app to run.

    This is my ObjectToIndexConverter, maybe the mistake is here:

    namespace Xamarin.FormsBook.Toolkit
    {
        [ContentProperty("Items")]
    
        public class ObjectToIndexConverter<T> : IValueConverter
        {
            public IList<T> Items { get; set; }
    
            public ObjectToIndexConverter()
            {
                Items = new List<T>();
            }
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null || !(value is T) || Items == null)
                    return -1;
    
                return Items.IndexOf((T)value);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                int index = (int)value;
    
                if (index < 0 || Items == null || index >= Items.Count)
                    return null;
    
                return Items[index];
            }
        }
    }
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    I see. I'm trying to work out how the XAML works. I've not come across this way of using SelectedIndex for a Picker.

    If you want to change the font size of a Label using a Picker, my approach would be:

    Create a simple object to hold a font size and description:

    public class FontSizeItem()
    {
        string Description {get; set;}
        double Size {get; set;}
    }
    

    Bind the Picker to a List of FontSizeItems.

    Have a public SelectedFontSizeItem property on your PageModel, which is updates as an is selected in the Picker.

    Bind the Label's FontSize to the SelectedFontSizeItem.Size.

    Of course I may be missing the aim of the above code, and also missing a more elegant way to do it all in XAML with Converters etc.

Sign In or Register to comment.