locked
Another vb error. What does it mean? RRS feed

  • Question

  • I get a vb error that says "Object reference not set to an instance of an object." The code Form4.Close() was highlighted.

    My code is:

    Module Module1
        Public numberAttempts As Integer
        Public World1Level1 As Boolean
        Public World1Level2 As Boolean
        Public World1Level3 As Boolean
        Public World1Level4 As Boolean
        Public World1Level5 As Boolean
        Public World2Level1 As Boolean
        Public World2Level2 As Boolean
        Public World2Level3 As Boolean
        Public World2Level4 As Boolean
        Public World2Level5 As Boolean
        Public World3Level1 As Boolean
        Public World3Level2 As Boolean
        Public World3Level3 As Boolean
        Public Boss1 As Boolean
        Public SecretLevel1 As Boolean
        Public SecretLevel2 As Boolean
    
        Sub NoAttempts()
            If numberAttempts = 0 Then
                MsgBox("You have ran out of lives!!!")
            End If
        End Sub
    
        Sub CloseAll()
            Form1.Close()
            Form2.Close()
            Form3.Close()
            Form4.Close()
            IntroForm.Close()
            Instructions.Close()
        End Sub
    End Module

    What does the error mean?

    Regards,

    Ali Hamza Mohammed

    Thursday, December 6, 2012 6:10 PM

Answers

  • Hi,

    Form4 is Nothing (doesn't point to an object) so you can't call its close method. See http://msdn.microsoft.com/en-us/library/sxw2ez55(v=vs.80).aspx (part of troubleshooting exceptions)


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, December 6, 2012 6:16 PM
  • You are going to need to look at that error more closely when it is thrown, the code that you posted honestly doesn't reveal much in the way of troubleshooting your problem. But I can tell you exactly what to look for:

    When you get your error message, look at the stack trace to see which line your error is caused on.

    That will be the Object reference that is not set to an 'Instance' of an object. This error is caused because somewhere you declare an object, but you do not declare it using a constructor:

    Example:

    Bad:

    Dim Button1 As Button

    Good:

    Dim Button1 As New Button

    Now it doesn't have to be a button, it is whatever object you are accessing in the line of code in your stack trace. Hope this helps ;)


    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Thursday, December 6, 2012 6:20 PM

All replies

  • Hi,

    Form4 is Nothing (doesn't point to an object) so you can't call its close method. See http://msdn.microsoft.com/en-us/library/sxw2ez55(v=vs.80).aspx (part of troubleshooting exceptions)


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, December 6, 2012 6:16 PM
  • You are going to need to look at that error more closely when it is thrown, the code that you posted honestly doesn't reveal much in the way of troubleshooting your problem. But I can tell you exactly what to look for:

    When you get your error message, look at the stack trace to see which line your error is caused on.

    That will be the Object reference that is not set to an 'Instance' of an object. This error is caused because somewhere you declare an object, but you do not declare it using a constructor:

    Example:

    Bad:

    Dim Button1 As Button

    Good:

    Dim Button1 As New Button

    Now it doesn't have to be a button, it is whatever object you are accessing in the line of code in your stack trace. Hope this helps ;)


    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Thursday, December 6, 2012 6:20 PM

  • Sub CloseAll() Form1.Close() Form2.Close() Form3.Close() Form4.Close() IntroForm.Close() Instructions.Close() End Sub

    I also want to add that instead of using the above code to close all of your forms and your application, you can accomplish that with a much simpler call:

    Application.Exit


    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Thursday, December 6, 2012 6:24 PM
  • Hi,

    Form4 is Nothing (doesn't point to an object) so you can't call its close method. See http://msdn.microsoft.com/en-us/library/sxw2ez55(v=vs.80).aspx (part of troubleshooting exceptions)


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Form 4 is an object in my project. I don't get the error if there is already a object in the project.

    Regards,

    Ali Hamza Mohammed

    Thursday, December 6, 2012 8:50 PM
  • Not sure what you meant. This error means that you try to use an unitialized object variable. As it happens on the Form4.Close line it would mean that Form4 is nothing. Check Form4 value with the debugger.

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, December 7, 2012 12:58 PM
  • Oh righ...

    Now I get you!

    Thanks for the help anyways,

    Regards,

    Ali Hamza Mohammed

    Thursday, December 13, 2012 7:32 PM