Error - Object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Error - Object reference not set to an instance of an object

    Hi

    I have a fairly large form with many labels and boxes etc. Some labels are set to alter their text onclick.

    It loads seemingly ok. However when I click the label the form crashes with the aforementioned error.

    The label is visible in design view. It is listed correctly in the document outline. Yet it is claimed it doesn't know about it. I've tried eliminating all 'load' events and have commented out alot of other classes but to no avail. The same code works ok on a very, very basic form so I assume it is getting bypassed by another class.

    Is there some known issue/workaround/code for this type of thing for a complex form? I can wait ages after loading the form before triggering the text change and it still fails.

    The code for the Label change is as follows.

    Code:
        Private Sub doText(ByVal whichlabel As String)
            Dim labelcontrol = "labelname" & whichlabel
            If Me.Controls(labelcontrol).Text = "Hello" Then
                Me.Controls(labelcontrol).Text = ""
            Else
                Me.Controls(labelcontrol).Text = "Hello"
            End If
        End Sub
    Many thanks!
  • Mikkeee
    New Member
    • Feb 2013
    • 94

    #2
    robertybob, I'm not sure if this is your issue or not but in VB6 there is no Text property of a label. Text boxes have the Text property, Labels have a Caption property.

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      Hi Mikkeee,

      I think this might be in the wrong forum as I'm using Visual Basic 2010 - I'll see if I can move it over.

      All the best.

      Comment

      • Mikkeee
        New Member
        • Feb 2013
        • 94

        #4
        robertybob, what exactly are you trying to accomplish? Are you new to vb.net (maybe coming from the vb6 world)? The reason I ask is that I believe you might be going about this the wrong way using more of a VB6 mindset. In dotnet you can assign many like controls to the same event and grab the incoming control from the sender property. Take a look at my sample below. I have three labels on a form and they will change when clicking on it. All you need to do is include additional control events after the Handles statement. This way you won't need to go through the forms control collection every time.

        Code:
            Private Sub LabelControl_Click(sender As System.Object, e As System.EventArgs) Handles LabelControl1.Click, LabelControl2.Click, LabelControl3.Click
                Dim labelIn As Label = DirectCast(sender, Label)
                labelIn.Text = String.Format("Hello from {0}", labelIn.Name)
            End Sub

        Comment

        • robertybob
          New Member
          • Feb 2013
          • 116

          #5
          Thanks Mikkeee - I am indeed completely new to VB but from a PHP background rather than VB6. There is, as you point out, a different mindset needed for each and I'm slowly learning best practises etc.

          What this code needs to accomplish is that users can visually see the state of something via the text that is displayed on the label (or color of text) and the processes triggered by the form know the state of the choices via the text displayed. I realise there are other methods such as radio/checkbox/combo etc to do this but plain text labels fit the look better in this case.

          I've tried the code you sent and this does indeed alter the text as intended so I will try to amend the rest to see if this can solve things completely - but is a good start so thanks very much.

          The odd thing I found with the Me.Controls version is that even if I commented out everything else in the class except the click event and the sub to alter the text, it still crashed even tho basic tests on other forms with same code worked. Not sure what can cause such a conflict but let me see if your code can be used in all the various ways needed.

          Many thanks again.

          Comment

          • robertybob
            New Member
            • Feb 2013
            • 116

            #6
            Hi Mikkeee,

            This code works really well in certain instances but I still have loads of cases where I'd have to target a particular numbered label (eg label1 or label2) dependent on certain calculations etc.

            I've tried your previous code

            Code:
            Dim labelcontrol = "label" & xx
            Dim thistext = Me.Controls(labelcontrol).Text
            and

            Code:
            Dim labelcontrol = "label" & xx
            Dim thislabel As Label = DirectCast(Me.Controls(labelcontrol), Label)
            Dim thistext = thislabel.Text
            Both these work on other forms but not current one - so I'm now at a loss to work out what can cause this to fail on one form and not others? Something somewhere is able to prevent this form from knowing that these labels exist. As I said, even if I comment out all other code it seems it doesn't help so it can't be a conflict with another class on the form.

            Any ideas what can cause this type of issue?

            Thanks!

            Comment

            • Mikkeee
              New Member
              • Feb 2013
              • 94

              #7
              Place a breakpoint on line #2 in your second example. In the immediate window see if you can directly reference the label.
              Code:
              ? label1.Text

              Comment

              • robertybob
                New Member
                • Feb 2013
                • 116

                #8
                This code (? label1.text) wasn't accepted by VB 2010 but I can say that all parts of the following code work fine on a test form Load event.

                However on the real form Load event (with all other classes commented out) it does nothing after alerting the name its looking for. No text appears on the Label. The only differences I can see in the build of the forms etc are..

                a) the real form is opened via a Form.Show() on a seperate form and the small test form is a standalone.

                b) the real form initial label text property is blank (ie "")

                Code:
                Dim xx = 1
                Dim thislabel = "Label" & xx
                MsgBox(thislabel)
                Dim thisbox = "Checkbox" & xx
                Me.Controls(thislabel).Text = "New Text"
                DirectCast(Me.Controls(thisbox), CheckBox).Checked = True
                Dim newlabel As Label = DirectCast(Me.Controls(thislabel), Label)
                Dim thistext = newlabel.Text
                MsgBox(thistext)
                newlabel.Text = "Changed Text"

                Comment

                • robertybob
                  New Member
                  • Feb 2013
                  • 116

                  #9
                  To clarify - the above code now does NOT cause any runtime failure, it just doesn't change the label text. :) Not sure why but it is now directly in the Load event rather than being called within a seperate sub

                  Comment

                  • Mikkeee
                    New Member
                    • Feb 2013
                    • 94

                    #10
                    Take the variables out of play. What happens if:
                    Code:
                    Label1.Text = "Text has changed"
                    Also, how are these labels being placed in the form? Are you placing them there at design time or using Me.Controls.Add at runtime?

                    Comment

                    • robertybob
                      New Member
                      • Feb 2013
                      • 116

                      #11
                      Adding a line Label1.Text = "ABC" at the start works fine. Then the label name is generated ok for the popup, then nothing. On the small test form all is fine inc the ABC.

                      All Labels are there at design time, tho alot have blank text. None are added on the fly at runtime with Me.Controls.Add

                      Thanks

                      Edit: is there any issues with how the form might be opened with Show() or would it have to import anything form the parent? Other forms I have seem to be ok without but then I don't think I'm using this controls method on those so far.

                      Comment

                      • Mikkeee
                        New Member
                        • Feb 2013
                        • 94

                        #12
                        It should be working. There's something in your project that I can't see that's causing this issue. I just thought of something... are your labels placed directly on the form or are they on a panel/group/container? If so, you will need to change the Me.Controls to ContainerName.C ontrols.

                        Comment

                        • robertybob
                          New Member
                          • Feb 2013
                          • 116

                          #13
                          Ah - the ones I'm looking at are within a table layout on the real form but not on the test form. Should this be <TableName>.Con trols? Will check this out. Many thanks.

                          Comment

                          • robertybob
                            New Member
                            • Feb 2013
                            • 116

                            #14
                            This looks like it was the issue. I'm still getting used to the structural requirements of VB so didn't occur to me that the label placement would be relevant.

                            Many thanks again Mikkeee for your persistence and lateral thinking :)

                            The only thing I now have is the targeting of variables set in the code - eg Public name1 As String - as these don't seem to be found with Me.Controls and not sure DirectCast is correct. Not sure what the equivalent would be for targetting these but can work around that for now.

                            Comment

                            • Mikkeee
                              New Member
                              • Feb 2013
                              • 94

                              #15
                              And this is another topic. This all depends on where you're placing these public variables. They are not controls so you won't find them under the control collection. Everything (variables and controls) are always found in their parent class. So... If you had a variable named MyVarName defined in a class named MyClass, you would find that variable under MyClass.MyVarNa me.

                              Comment

                              Working...