Solved

Getting error on WPF application?

Posted on 2014-12-24
5
148 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 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 63

Expert Comment

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

Featured Post

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Code enhancement 4 44
Access Schema 6 53
Consume a webservice via VB in Visual Studio 2015 3 42
VB.Net Data Class 1 44
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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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