Object reference not set to an instance of an object. Please help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coopsterdude
    New Member
    • Dec 2007
    • 6

    Object reference not set to an instance of an object. Please help!

    Hi, my program compiles ok but when I click on a the control to execute this:

    Public Sub numberUpdate(By Val theNum As Integer)



    lblCurrentQuest ionNumber.Text = theNum


    End Sub

    I get this error message: System.NullRefe renceException: Object reference not set to an instance of an object.

    Can anyone help me with this please?
  • emilnordiclake
    New Member
    • Dec 2007
    • 2

    #2
    You need to assign a value to theNum before passing it to the function.

    For example:

    theNum = 123

    Comment

    • CyberSoftHari
      Recognized Expert Contributor
      • Sep 2007
      • 488

      #3
      Can you point here how you are calling above method? That will be helpful to describe the problem.

      Comment

      • coopsterdude
        New Member
        • Dec 2007
        • 6

        #4
        Here is what my code looks like that I'm using to call:

        Dim x As _default
        x = New _default
        Dim intSentNum As Integer
        intSentNum = 1
        x.numberUpdate( intSentNum)

        Here's the sub that I call:

        Public Sub numberUpdate(By Val inComingNum As Integer)

        lblCurrentQuest ionNumber.Text = inComingNum


        End Sub

        For some reason, I get the same object reference error message.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Is lblCurrentQuest ionNumber declared correctly?
          [code=vbnet]
          Dim lblCurrentQuest ionNumber as Label
          lblCurrentQuest ionNumber = new Label
          [/code]

          Comment

          • coopsterdude
            New Member
            • Dec 2007
            • 6

            #6
            lblCurrentQuest ionNumber is a control on the form. I did this to test your idea:

            Dim lblCurrentQuest ionNumber As Label
            lblCurrentQuest ionNumber = New Label
            lblCurrentQuest ionNumber.Text = "e"

            the code seems to create another label dynamically but it doesn’t access my label with the same name which is already on the form.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Hmm, does it correct itself if you use:
              lblCurrentQuest ionNumber.Text = inComingNum.ToS tring

              Comment

              • coopsterdude
                New Member
                • Dec 2007
                • 6

                #8
                I’m afraid I’ve tried that. I get the error message only when I try to assign a value to a property of the control. Even if I do this:

                lblCurrentQuest ionNumber.text= ”hello”

                I still get the error message

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  You are not calling this function BEFORE the InitializeCompo nent function are you? That would have to happen FIRST in order for the controls to be created properly.

                  Comment

                  • coopsterdude
                    New Member
                    • Dec 2007
                    • 6

                    #10
                    I created the control by dragging it onto the form so I think .NET takes care of any initializing issues. I can set the property of the control from a button that's on the form. I'm thinking of trying to call this button procedure and try to access the control this way. When I call a button, do you know what I need to provide for these parameters?

                    butNext_Click(B yVal sender As System.Object, ByVal e As System.EventArg s)

                    If I can successfully access the code inside this sub then I think I can troubleshoot the other problem.

                    Thanks for all your help!!

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      .NET does take care of the initialization stuff, by calling InitializeCompo nent in the constructor class for your form. So that would have to happen first.
                      Code:
                      public Form1()
                              {
                                  InitializeComponent();
                              }
                      If your function does not get called before that function does, I'm not sure what to tell you.

                      Comment

                      • coopsterdude
                        New Member
                        • Dec 2007
                        • 6

                        #12
                        I called the button successfully but got the same error. If I click on a button that executes the code, it works fine. When I try to execute that same line of code I get an error.

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          How are you calling that function in code? and WHERE in your code are you calling it?
                          You cannot access your windows controls until after that designer code function gets executed.

                          Comment

                          • CyberSoftHari
                            Recognized Expert Contributor
                            • Sep 2007
                            • 488

                            #14
                            as in #4
                            [CODE=cpp]//You are declaring the x here
                            Dim x As _default
                            /* And initializeing the X in below decleration or some where else? If Yes then you can use like Dim x As new _default, If no Here is the mistake happening because before calling the method where you initializing the x you are calling numberUpdate(By Val inComingNum As Integer) */
                            x = New _default[/CODE]

                            Better I suggest to keep a break point and debug it.

                            Comment

                            • priyamtheone
                              New Member
                              • Sep 2007
                              • 88

                              #15
                              Originally posted by coopsterdude
                              I called the button successfully but got the same error. If I click on a button that executes the code, it works fine. When I try to execute that same line of code I get an error.

                              If ur idea is to write the code in butNext_Click event and call this event from elsewhere then U gotto call it in the following way:

                              dim a as System.Object
                              dim b as System.EventArg s
                              butNext_Click(a ,b)

                              Comment

                              Working...