# Now this is weird

This topic is 3849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
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.

##### Share on other sites
Maybe you need to create the BoxRef?

BoxRef = New NumericUpDown()

At least that is how it worked in MSDN sample :
MSDN

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002308
×