Sign in to follow this  

Now this is weird

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

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this