Results 1 to 5 of 5

Thread: [RESOLVED] System.Null ReferenceException : Object reference not set to an instance of an object

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    26

    Resolved [RESOLVED] System.Null ReferenceException : Object reference not set to an instance of an object

    Currently working on the status bar for application. I would like the status bar to display message errors if a user makes an invalid entry.
    I have a module which contains the main procedure and procedures to change the text property of my status bar. A Call is made to the procedures from one of the forms that requires the user input. The application compliles but I get an unhandled exception of type 'System.Null ReferenceException' Object reference not set to an instance of an object.

    I have relevant code is shown below:

    Code:
    Module MainMod
    02
     
    03
        Public sErrorMessage As String
    04
     
    05
        Private frmFinancialCalculations As frmFinancialCalculations
    06
     
    07
        Public Sub Main()
    08
     
    09
            Application.Run(frmFinancialCalculations)
    10
     
    11
        End Sub
    12
     
    13
        Public Sub DisplayErrorMessage()
    14
     
    15
            frmFinancialCalculations.sbFinancialCalculations.Text = sErrorMessage 'This is where exception is happening
    16
     
    17
            MessageBox.Show(sErrorMessage, "Financial Calculations")
    18
     
    19
        End Sub
    20
        Public Sub ClearErrorMessage()
    21
     
    22
            frmFinancialCalculations.sbFinancialCalculations.Text = ""
    23
     
    24
        End Sub
    25
    End Module
    26
     
    27
    ===================================================================================
    28
     
    29
     
    30
    Private Sub btnCalculate_Click(ByVal sender As System.Object, _
    31
            ByVal e As System.EventArgs) Handles btnCalculate.Click
    32
     
    33
            If rdoFutureValue.Checked = True Then ' Determine which radio buton is called
    34
                If ValidEntry(txtMonthlyInvestment.Text, 100, 1000) Then
    35
                    If ValidEntry(txtInterestRate.Text, 0, 24) Then
    36
                        If ValidEntry(txtYears.Text, 1, 65) Then
    37
                            Me.CalculateFutureValue()
    38
                            txtFutureValue.Focus()
    39
                        Else
    40
                            sErrorMessage = "Years must be  a number between 1 and 65."
    41
                            Call DisplayErrorMessage()
    42
                            'MessageBox.Show("Years must be  a number between 1 and 65.", _
    43
                            '"Entry error")
    44
                            txtYears.Focus()
    45
                        End If
    46
                    Else
    47
                        sErrorMessage = "Interest rate must be a number between 0 and 24."
    48
                        Call DisplayErrorMessage()
    49
                        'MessageBox.Show("Interest rate must be a number between 0 and 24.", _
    50
                        ' "Entry error")
    51
                        txtInterestRate.Focus()
    52
                    End If
    53
                Else
    54
                    sErrorMessage = "Monthly investment must be a number between 100 and 1,000."
    55
                    Call DisplayErrorMessage()
    56
                    'MessageBox.Show("Monthly investment must be a number between 100 and 1,000.", _
    57
                    '"Entry error")
    58
                    txtMonthlyInvestment.Focus()
    59
                End If
    60
            ElseIf rdoMonthlyInvestment.Checked = True Then
    61
                If ValidEntry(txtInterestRate.Text, 0, 24) Then
    62
                    If ValidEntry(txtYears.Text, 1, 65) Then
    63
                        If ValidEntry(txtFutureValue.Text, 1000, 100000) Then
    64
                            Me.CalculateMonthlyInvestment()
    65
                            txtMonthlyInvestment.Focus()
    66
                        Else
    67
                            sErrorMessage = "Future Value must be a number between 1,000 and 1,000,00."
    68
                            Call DisplayErrorMessage()
    69
                            'MessageBox.Show("Future Value must be a number between 1,000 and 1,000,00.", _
    70
                            '"Entry error")
    71
                            txtFutureValue.Focus()
    72
                        End If
    73
                    Else
    74
                        sErrorMessage = "Interest rate must be a number between 0 and 24."
    75
                        Call DisplayErrorMessage()
    76
                        'MessageBox.Show("Interest rate must be a number between 0 and 24.", _
    77
                        '"Entry error")
    78
                        txtInterestRate.Focus()
    79
                    End If
    80
                Else
    81
                    sErrorMessage = "Years must be  a number between 1 and 65."
    82
                    Call DisplayErrorMessage()
    83
                    'MessageBox.Show("Years must be  a number between 1 and 65.", _
    84
                    '"Entry error")
    85
                    txtYears.Focus()
    86
                End If
    87
            Else
    88
                sErrorMessage = "Years must be  a number between 1 and 65."
    89
                Call DisplayErrorMessage()
    90
                'MessageBox.Show("Please specify the calculation as Future Value or Monthly investment", _
    91
                '"Entry error")
    92
                GroupBox1.Focus() ' If no radio buttons are selected
    93
     
    94
            End If
    95
        End Sub
    Line 15 above is where the exception is occuring but I am not sure why. Any ideas?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: System.Null ReferenceException : Object reference not set to an instance of an ob

    The error is on line 5:

    Private frmFinancialCalculations As frmFinancialCalculations

    Needs to be:

    Private frmFinancialCalculations As New frmFinancialCalculations

    Otherwise you are just creating the space that will hold the form without actually creating the form to put into that space.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    26

    Re: System.Null ReferenceException : Object reference not set to an instance of an ob

    Thanks for that. I have spent the best part of 4 hours staring at the code trying to sort this out lol

    cheers

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: System.Null ReferenceException : Object reference not set to an instance of an ob

    When you get that particular error message, highlight each object or method that returns an object, in the line that throws the exception. One of them will be Nothing, and that is where your problem is. The line you said was throwing the exception was this one:

    frmFinancialCalculations.sbFinancialCalculations.Text = sErrorMessage

    The problem can't be in sErrorMessage, because it wouldn't make any difference if that was Nothing, since you aren't accessing any members of it. Therefore, what remains are these two:

    frmFinancialCalculations and frmFinancialCalculations.sbFinancialCalculations

    (the .Text is just a property of sbFinancialCalculations, and can be ignored because you are not accessing any members of it)

    The first of those is obviously a form, while the second appears to be a member of the form. If either one was Nothing, then you would get that exception as you are accessing members of an object that doesn't exist. I first looked at your code to see how the form was declared, and saw right away that there was no New. That wasn't conclusive, as you could have been creating the instance elsewhere. However, the only safe place to be creating the instance was in Sub Main, and you weren't creating it there. Since no other place was suitable, it was clear that if that was not the problem in this specific case, it was still a problem overall.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    26

    Re: System.Null ReferenceException : Object reference not set to an instance of an ob

    Many thanks for that explanation. I have been going through Murachs beginning .net for 2 weeks. Thus I am just a beginner. Whilst I find the book quite informative, it doesn't give much help if any exceptions are thrown with the sample applications. What you have said makes perfect sense based on the knowledge I have gained so far. Many thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width