C#: Accessing variable raises System.NullReferenceException

LalaRiverLalaRiver ✭✭DEMember ✭✭
edited November 2017 in General

I have converted my XAML file to C#.
The problem that I'm having is that the line "grid.Children.Clear()" raises a System.NullReferenceException:

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        grid.Children.Clear(); //raises Null exception
            }

I've declared this variable as "private Grid grid;", so it should be accessible, I think.

And

                    public PageCanvas()
        {

is called.

What I don't know is if "grid = new Grid" is actually called because the debugger doesn't get into the substep.

Would anybody have a look at my code and see if he spots any error?
I just don't see it.

Thank you very much.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Version.Plugin;
using Xamarin.Forms;

namespace MyApp
{
    public class PageCanvas : ContentPage
        {

            private Grid grid;

            public PageCanvas()
        {
            Title = "TestApp";

            var toolbarItem = new ToolbarItem
            {
                Text = "bla",
                Icon = Device.OnPlatform(null, "plus.png", "plus.png"),
            };
            toolbarItem.Clicked += async (sender, e) =>
            {
                await Navigation.PushAsync(new CellEditorSurface
                {
                    BindingContext = new Cell()
                });
            };
            ToolbarItems.Add(toolbarItem);

            var _lv = new ListView
            {
                Margin = new Thickness(20),
                ItemTemplate = new DataTemplate(() =>
                {
                    var label = new Label
                    {
                        VerticalTextAlignment = TextAlignment.Center,
                        HorizontalOptions = LayoutOptions.StartAndExpand
                    };
                    label.SetBinding(Label.TextProperty, "Name");

                    var tick = new Xamarin.Forms.Image
                    {
                        Source = ImageSource.FromFile("check.png"),
                        HorizontalOptions = LayoutOptions.End
                    };
                    tick.SetBinding(VisualElement.IsVisibleProperty, "Done");

        grid = new Grid
        {
                        Margin = new Thickness(20, 20, 20, 20),
                        BackgroundColor = Color.Red,
                };

        var stackLayout = new StackLayout
        {
            Margin = new Thickness(20, 0, 0, 0),
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            Children = { label, tick, grid }
        };

        return new ViewCell { View = stackLayout };
        })
    };
        _lv.ItemSelected += async (sender, e) =>
        {
            await Navigation.PushAsync(new CellEditorSurface
            {
                BindingContext = e.SelectedItem as Cell
            });
        };
        Content = _lv;
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        grid.Children.Clear(); //raises Null exception
        }
        private async void OnItemSelected(object sender, EventArgs e)
        {
        return;
        }

        async void OnItemAdded(object sender, EventArgs e)
        {
        return;
        }

}
Tagged:

Best Answer

  • LalaRiverLalaRiver ✭✭ DE ✭✭
    Accepted Answer

    @AlessandroCaliaro said:
    Why you should Clear a grid?
    Second, it seems your grid has no children.

    Thank you. I've written the code from scratch now, and I do it completely different, and it works now.
    I don't use "return new ViewCell { View = stackLayout };" anymore at all.
    I don't know what ViewCell actually did, and my code works fine without it.

    I don't clear the grid's children at OnAppearance in my app, I just posted a simplified version of my code to isolate the problem.

Answers

  • AlessandroCaliaroAlessandroCaliaro ✭✭✭✭✭ ITMember ✭✭✭✭✭
    edited November 2017

    Why you should Clear a grid?
    Second, it seems your grid has no children.

  • LalaRiverLalaRiver ✭✭ DEMember ✭✭

    Ok, the problem seems to be "return new ViewCell { View = stackLayout };" but I don't know how I could do the same without returning.

  • LalaRiverLalaRiver ✭✭ DEMember ✭✭
    Accepted Answer

    @AlessandroCaliaro said:
    Why you should Clear a grid?
    Second, it seems your grid has no children.

    Thank you. I've written the code from scratch now, and I do it completely different, and it works now.
    I don't use "return new ViewCell { View = stackLayout };" anymore at all.
    I don't know what ViewCell actually did, and my code works fine without it.

    I don't clear the grid's children at OnAppearance in my app, I just posted a simplified version of my code to isolate the problem.

Sign In or Register to comment.