MDI form Error

Posted on 2004-11-11
Last Modified: 2010-04-24
I'm getting the following error occasionally when I load my initial MDI form:

"Object reference not set to an instance of an object".

It happens on the following line of code:

            frmMain.ActiveForm.Text = "Centre : " & BranchName & " (" & UserCentre & "); User: " UserFullName & " (" & LCase(Username) & ")"

It doesn't happen all the time the application is loaded which makes it very hard to figure out why the eroror is occuring. Does anyone have any ideas?
Question by:PeterErhard
    LVL 3

    Expert Comment

    break the line down into
    frmMain.ActiveForm.Text = "Centre : "
    frmMain.ActiveForm.Text = frmMain.ActiveForm.Text + BranchName
    frmMain.ActiveForm.Text = frmMain.ActiveForm.Text + " (" + UserCentre
    .... etc

    to find out which variable isnt getting initialised properly.
    LVL 85

    Accepted Solution

    The ActiveForm property may be returning Nothing if the application is still loading and no form is yet active:

        "A Form that represents the currently active form, or a null reference (Nothing in Visual Basic) if there is no active form."

    Where are you executing this line of code from?

        Dim f As Form = frmMain.ActiveForm
        If Not (f Is Nothing) Then
            f.Text = "Centre : " & BranchName & " (" & UserCentre & "); User: " UserFullName & " (" & LCase(Username) & ")"
            MsgBox "No Active Form"
        End If

    Instead of changing the form text is this manner, why not place code in that forms load event?


    Author Comment

    Thanks for that - appears to have worked!

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    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…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    731 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

    15 Experts available now in Live!

    Get 1:1 Help Now