0

I am currently assigning an Event Handler to an Event, but I did need to pass one other argument to know what produced this event. It is in a function to set up a XAML file. I did something like that :

chart[i].PaintSurface += (sender, e) => OnCanvasViewPaintSurface(sender, e, i);

But this same line is throwing a Null Reference Exception.

I looked down on the web to know why, and most of the times it's because the object isn't initialised, but here it is.

Here is the function in which occurs this problem. The array chart is a global variable.

 private void CreateUIElements()
        {
            int MAXCHANNELS = (int)App.UserPrefs[UserPreferenceReference.TotalChannels];
            ChannelLabels = new Label[MAXCHANNELS];
            Plotlayouts = new AbsoluteLayout[MAXCHANNELS];
            for (int i = 0; i < MAXCHANNELS; i++)
            {
                MainGrid.RowDefinitions.Add(new RowDefinition { Height = 15 });
                if (i != MAXCHANNELS - 1)
                {
                    MainGrid.RowDefinitions.Add(new RowDefinition { Height = 75 });
                }
                else
                {
                    MainGrid.RowDefinitions.Add(new RowDefinition { Height = 100 });
                }

                ChannelLabels[i] = new Label()
                {
                    Text = "Channel" + (i + 1),
                    TextColor = Color.Gray
                };
                MainGrid.Children.Add(ChannelLabels[i], 0, 2 * i);

                Plotlayouts[i] = new AbsoluteLayout();
                chart[i] = new SKCanvasView
                {
                    BackgroundColor = Color.White,
                    VerticalOptions = LayoutOptions.FillAndExpand,   // stretch the view
                    HorizontalOptions = LayoutOptions.FillAndExpand, // stretch the view

                    // WidthRequest = 100,  // specify a size
                    // HeightRequest = 100, // specify a size
                };

                chart[i].PaintSurface += (sender, e) => OnCanvasViewPaintSurface(sender, e, i);


                Plotlayouts[i].Children.Add(chart[i], new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All);
                MainGrid.Children.Add(Plotlayouts[i], 0, 2 * i + 1);

            }
            MainGrid.RowDefinitions.Add(new RowDefinition { Height = 50 });

            var buttonLayout = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.Center
            };

            MainGrid.Children.Add(buttonLayout, 0, MAXCHANNELS);
        }

Thank you very much for your help.

  • This is very simple, just don't call anything on a null object and the issue is resolved – Mrinal Kamboj Aug 09 '19 at 09:40
  • An NRE is *always* thrown because something wasn't initialized. The exception's call stack shows which function actually threw. How is `PaintSurface` declared? Is it initialized? `+=` is a shortcut for `AddHandler`, it doesn't initialize an event property – Panagiotis Kanavos Aug 09 '19 at 09:42
  • PaintSurface is an event in Skiasharp, I really don't know how it is declared. Do I have to initialise it ? – florent SERFAS Aug 09 '19 at 10:18
  • I tested that exact same line in an other file where the SKCanvasView is instantiated in the XAML file, do you think that can change anything ? – florent SERFAS Aug 12 '19 at 08:19

0 Answers0