Here's a good one for everyone. I have been working on a Screensaver program for some time now (some of you may even remember questions related to this). Anyway, one of the biggest challanenges with developing it was finding a good way to transition images, smoothly, and not killing all my resources. I ended up using a WPF for this piece that is used to fade in and out images.

Once I had that little component developed, I created a new class, an object that inherited a Windows Form Panel, and then proceeded to add on a series of functions. This item includes housing the WPF I have build. The code looks like this (these are snippets)

//Fields setup for the Panel
private ElementHost WPFHost;
private ScreenSaverImageDisplay ImageDisplayer;

//Inside the custom Panel's initializer
this.ImageDisplayer = new ScreenSaverImageDisplay(new ImageFadeComplete(ImageFadeCompleteCheckIn));
this.ImageDisplayer.Size = new System.Windows.Size(Width, Height);

this.WPFHost = new ElementHost();
this.WPFHost.Location = new Point(0, 0);
this.WPFHost.Size = new Size(Width, Height);
this.WPFHost.Child = ImageDisplayer;
this.Controls.Add(WPFHost);

That's how I setup and initialize the thing, pretty simple.

However, while the code runs fine all the time when I debug through Visual Studio, there is this issue I have when I run it as an actual ScreenSaver (you know go to your properties choose it, and having it setup to start after so much time idle). The problem I am facing is that sometimes (I can't seem to find a pattern), is a null exception will be thrown on the start of the ScreenSaver. Again it doesn't happy everytime, and I can't even be sure if it only happens the first time it tries to run (that being like if I shut down the PC and start it back up, that would count as a first time when it runs the first time).

I get the usual exception window, and this

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.Integration.ElementHost.Select(Boolean directed, Boolean forward)
at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.WmActivate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I can not figure out what is causing this problem. This is all internal code, as I do not have any functions or anything like this. And like I said, I can never get this to occur when I am debugging so it makes it even more difficult to pin down.

I am hoping maybe someone has seen this before and knows what's causing it, because right now I am scratching my head. Null exceptions can be tricky, but this one is a stumper, even more because I have no clue where in my code this is occuring.

Thanks everyone for the help

In your constructor for your class, do you instantiate all needed objects from the WPF?

There is one piece I have found that could blow the error.

If you notice above I assign a delegate called "ImageFadeComplete", which calls back to a function containing a timer in it. That timer was initialized after the code above. Is there any possibility that code was calling back to the delegate function? I thought delegates resides in the class the code resides, and not an instance related to the object though

Not leaning heavily to that though, cause I feel the error would be else where.

By the way when you say constructor, do you mean the Panel or WPF?

Well bad news, the timer wasn't the fix.

What's so weird is when debugging I can never create this error (of course if it did I probably wouldn't be in the current stumper). When you mentioned the constructor I assume you were talking abou the WPF's correct?

Alright I think I am going to mark this solved, as I finally believe I have found the fix.

I was doing some other work on the project and started to realize some of my images were loading in with issues. Turns out I was trying to load images in on a thread, and then Invoking a control on the form to assign an image to.

The problem I saw was that the component's Handler wasn't actually created yet, and causing null errors to below (or like that from what I can tell, I don't recall the exact error). I updated the code to pretty much no proceed with any tasks until the Handler is created for the component. So far all tests have proven successful. (now if I just knew exactly what the Handler did)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.