Now this is weird

Started by
1 comment, last by ToohrVyk 16 years, 3 months ago
Below are two snippets of code. The first works, but the second is claiming that "Object reference not set to an instance of an object." As near as I can tell, they're effectively identical, so why does the second one not work? They're written in Visual Basic 2005, and are part of my final project for Intro to Programming. Sad thing is I showed the teacher and he wasn't sure what I did in the first one!

            Dim picButton As System.Windows.Forms.PictureBox
            Dim lstButton As System.Windows.Forms.ListBox
            Do Until Placement > 26
                If Placement < 9 Then
                    picButton = Me.Controls("picSkill0" & (Placement + 1))
                    lstButton = Me.Controls("lstSkill0" & (Placement + 1))
                Else
                    picButton = Me.Controls("picSkill" & (Placement + 1))
                    lstButton = Me.Controls("lstSkill" & (Placement + 1))
                End If
                LoadButtonInfo(0, Placement, picButton, lstButton)
                Placement += 1
            Loop

        Dim BoxRef As System.Windows.Forms.NumericUpDown
        BoxRef = Me.Controls("numAccuracy")
        AttributeChange(0, BoxRef)
Ideas? I'm at a total loss, and searching for hours on the web has yielded nothing relating to this except mention of using something like "Set Boxref =" etc. Problem is when I try to prefix that line with Set, Visual Basic discards it again.
My name is "Todd". Drop the "T" and it explains sooooooo much! :P
Advertisement
Maybe you need to create the BoxRef?

BoxRef = New NumericUpDown()


At least that is how it worked in MSDN sample :
MSDN
Sincerely,Arto RuotsalainenDawn Bringer 3D - Tips & Tricks
Debug your program. There's nothing about debugging that requires any ounce of creativity or intuition, it's merely a process of backtracking through your code until you see the source of all symptoms.

In your case, the symptom is that a given variable is used when it's null. Is the variable expected to be null at that point? If it is, then why are you using it? If it isn't, then this is a symptom that the code which provides you with the variable doesn't work. I'll assume this is the second case here.

So, the new symptom is that Me.Controls("numAccuracy") returns a null when it shouldn't. Under which circumstances does this happen? This is where you read the documentation for that function and find out in what situations it returns null. If you expect to be in this situation, then the code is badly designed. If you don't expect this situation, then find out why it happened by reading the documentation and the previous uses of the object.

Go on like this until you encounter an obvious cause, which you then correct.

This topic is closed to new replies.

Advertisement