Solved

Getting error on WPF application?

Posted on 2014-12-24
5
139 Views
Last Modified: 2014-12-29
I'm writing a test application using WPF (which I've never used before). I'm getting the following error on the MainWindow_Loaded() Event.

"MainWindow_Loaded() - Object reference not set to an instance of an object."

I'm guessing WPF forms don't initialize like a Windows Form and I've found that I can't just double-click on the WPF Form and have the "MainWindow_Loaded()" Event appear in the Code-behind. Here is my code below:

     Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Try
            tmr.Interval = 1
            EyeClosed.Visibility = Windows.Visibility.Hidden

        Catch ex As Exception
            MessageBox.Show("MainWindow_Loaded() - " & ex.Message)
        End Try
    End Sub

Open in new window

0
Comment
Question by:BlakeMcKenna
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40516837
Hi BlakeMcKenna;

It would help to know on what line in the code you get the exception "Object reference not set to an instance of an object." on?
0
 

Author Comment

by:BlakeMcKenna
ID: 40516989
Actually Fernando...I'm not getting that error now, rather I'm getting a different one. I've added a Timer.Timer control to the app. I've noticed that the Timer control for a WPF app is different from a Windows Timer control in that the WPF TC doesn't have the Tick() Event only the Elapsed() Event. I set the "Interval" property to 1. The new error I'm betting occurs in the Elapsed() Event.

Class MainWindow 
    Private WithEvents tmr As New Timers.Timer

    Private blnStartBlinking As Boolean = False
    Private blnBlink As Boolean = False
    '
    '
    '
    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Try
            LEyeClosed.Visibility = Windows.Visibility.Collapsed
            REyeClosed.Visibility = Windows.Visibility.Collapsed

        Catch ex As Exception
            MessageBox.Show("MainWindow_Loaded() - " & ex.Message)
        End Try
    End Sub

    Private Sub tmr_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles tmr.Elapsed
        Try
            If blnBlink Then
                blnBlink = False
                LEyeOpened.Visibility = Windows.Visibility.Hidden
                LIris.Visibility = Windows.Visibility.Hidden
                LPupil.Visibility = Windows.Visibility.Hidden
                LEyeClosed.Visibility = Windows.Visibility.Visible

                REyeOpened.Visibility = Windows.Visibility.Hidden
                RIris.Visibility = Windows.Visibility.Hidden
                RPupil.Visibility = Windows.Visibility.Hidden
                REyeClosed.Visibility = Windows.Visibility.Visible
            Else
                blnBlink = True
                LEyeClosed.Visibility = Windows.Visibility.Hidden
                LEyeOpened.Visibility = Windows.Visibility.Visible
                LIris.Visibility = Windows.Visibility.Visible
                LPupil.Visibility = Windows.Visibility.Visible

                REyeClosed.Visibility = Windows.Visibility.Hidden
                REyeOpened.Visibility = Windows.Visibility.Visible
                RIris.Visibility = Windows.Visibility.Visible
                RPupil.Visibility = Windows.Visibility.Visible
            End If

        Catch ex As Exception
            MessageBox.Show("tmr_Elapsed() - " & ex.Message)
        End Try
    End Sub
    '
    '
    '
    Private Sub btnInitialize(sender As Object, e As RoutedEventArgs)
        Try
            tmr.Interval = 1

            LEyeClosed.Visibility = Windows.Visibility.Hidden
            LEyeOpened.Visibility = Windows.Visibility.Visible
            LIris.Visibility = Windows.Visibility.Visible
            LPupil.Visibility = Windows.Visibility.Visible

            REyeClosed.Visibility = Windows.Visibility.Hidden
            REyeOpened.Visibility = Windows.Visibility.Visible
            RIris.Visibility = Windows.Visibility.Visible
            RPupil.Visibility = Windows.Visibility.Visible

        Catch ex As Exception
            MessageBox.Show("btnInitialize() - " & ex.Message)
        End Try
    End Sub
    '
    '
    '
    Private Sub btnStartBlinking(sender As Object, e As RoutedEventArgs)
        tmr.Start()
    End Sub

    Private Sub btnStopBlinking(sender As Object, e As RoutedEventArgs)
        tmr.Stop()
    End Sub
End Class

Open in new window


The error message is:  "tmr_Elapsed() - The calling thread cannot access this object because a different thread owns it."
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40517029
I believe you should be using the DispatcherTimer Class in WPF. The timer you are using is the System Timer which is not running on the same thread. At the bottom of the page in the above link there is a sample of how to set it up.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40521892
Thanks Fernando...that was the issue...
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40521893
Not a problem, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now