?
Solved

Getting error on WPF application?

Posted on 2014-12-24
5
Medium Priority
?
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

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 63

Accepted Solution

by:
Fernando Soto earned 2000 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 63

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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