Getting NullReferenceException when using WPF on the touch display

Hi,

I had this all fine and running last night with touch etc. but now I’m getting the following error when I try and assign anything to display.WpfWindow.Child

Using mainboard GHIElectronics-FEZSpider version 1.0
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
    #### Message: 
    #### Microsoft.SPOT.Presentation.Media.MediaContext::RenderMessageHandler [IP: 00c5] ####
    #### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 004a] ####
    #### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001d] ####
    #### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
    #### Gadgeteer.Program::Run [IP: 0024] ####
    #### Bib_From_Space_Gadgeteer.Program::Main [IP: 0016] ####
A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.SPOT.TinyCore.dll

It doesn’t break in the code but in Gadgeteer’s Run() method, the program still runs but this is printed to the console and nothing is shown on the screen.

Here is my very simple Canvas test code:

anvas c = new Canvas();
            c.Width = 320;
            c.Height = 240;

            Rectangle r = new Rectangle(50, 50);
            r.Fill = new SolidColorBrush(Colors.Cyan);
            Canvas.SetLeft(r, 100);
            Canvas.SetTop(r, 100);
            c.Children.Add(r);

            display.WPFWindow.Child = c;
            
            display.WPFWindow.Child.Visibility = Visibility.Visible;

This was all working fine yesterday with many different canvases and touch events on rectangles and text.

Is there anything I could have done to cause this? All I added was a button to the designer and button handler. I’ve tried with the touch cable attached and with it detached and have swapped out the display for a spare one with and swapped out all of the cables.

Has anyone had this problem?

Thanks for your time,

Poncho

Hmm, I created a new project to try it out with only the Display connected and it seems the problem is with Rectangle.Fill.

If I create a canvas, add a rectangle and put this as display.WpfWindow.Child then it runs, the screen is blank but it runs.

If I add a fill to the Rectangle with:

rect.Fill = new SolidColorBrush(ColorUtility.ColorFromRGB(200, 200, 200));

I get the error I posted before.

The problem is, all yesterday I have been using Text, Rectangles etc. with both Fills and Strokes but now I’m getting this exception.

Is there something that I’m missing?

Thanks.

Hard to say. Can you post full method?

Sure,

The full Program.cs for the test Project I just created is:

public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Canvas c = new Canvas();
            c.Width = 320;
            c.Height = 240;

            Rectangle r = new Rectangle(100, 100);
            SolidColorBrush scb = new SolidColorBrush(ColorUtility.ColorFromRGB(200, 200, 200));
            r.Fill = scb;
            c.Children.Add(r);
            Canvas.SetTop(c.Children[0], 50);
            Canvas.SetLeft(c.Children[0], 50);

            display.WPFWindow.Child = c;
       }
    }

If you try that code it fails for me but if you comment out “r.Fill = scb;” it runs, theres nothing on the screen except a white background but it works.

Fill was working for yesterday :confused:

EDIT: Just tried stroke and getting the same problem.

Try to put

r.Fill = scb;

after you have added r to the Canvas.

Does it make any difference?

Nope :frowning:

Exactly the same error.

I’ve tried with ImageBrush and LinearGradientBrush but the same happens with those.

I suppose I could revert to using Simple Graphics but that means losing all of the canvases/panels etc. I’ve set up so far. Also I’m assuming SimpleGraphics doesnt work with touch input right?

Does my code work fine for you?

No, I haven’t tried it. I can try it later tonight, though.

I’m getting similar behaviours on version 4.2.
I strongly hope somebody could suggest a solution.
http://www.tinyclr.com/forum/topic?id=9427&page=1