Results 1 to 9 of 9

Thread: [Resolved]Object Instance Error

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    [Resolved]Object Instance Error

    VB Code:
    1. 'from the AlphaNumericButton usercontrol
    2. Public Function getChr() As String
    3.        If IsAlphabetic = False Then
    4.            getChr = strDigitValue
    5.        Else
    6.            If keyPadTimer.Enabled = True Then
    7.               'supposed to be working with this bit
    8.                Dim tmpArray() As String
    9.                Dim i As Integer
    10.                For i = 0 To Len(strAlphaValue)
    11.                    tmpArray(i) = strAlphaValue.Substring(i, 1)
    12.                Next
    13.                For i = 0 To tmpArray.Length
    14.                    MsgBox(tmpArray(i))
    15.                Next
    16.                getChr = ""
    17.                'end this bit
    18.            Else
    19.                If strAlphaValue.Length > 0 Then
    20.                    getChr = strAlphaValue.Substring(0, 1)
    21.                Else
    22.                    getChr = " "
    23.                End If
    24.            End If
    25.        End If
    26.    End Function
    27.  
    28.  
    29.  
    30.  
    31.  
    32. Private Sub btnKey2to9Handler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKey2.Click, btnKey3.Click
    33.  
    34.        If armed = True Or expectingInput = True Then
    35.            Static KeyValue As String
    36.            MsgBox(sender.name)
    37.            If Not lastKeyPressed Is sender Then
    38.                AlphaNumericButton.keyPadTimer.Enabled = False
    39.            End If
    40.            lastKeyPressed = sender
    41.  
    42.            If AlphaNumericButton.IsAlphabetic = False Then
    43.                KeyValue = sender.Getchr
    44.            Else
    45.  
    46.               'Working with this part
    47.                If Not AlphaNumericButton.keyPadTimer.Enabled Then
    48.                    KeyValue = sender.Getchr
    49.                    AlphaNumericButton.keyPadTimer.Enabled = True
    50.                Else
    51.                    'Delete the last alpha character.
    52.                    If inputBuffer.Length > 0 Then
    53.                        inputBuffer = inputBuffer.Substring(0, inputBuffer.Length - 1)
    54.                    End If
    55.                    'Determine the next alpha character.
    56.  
    57.                    'Errors here if I click within one second
    58.                    MsgBox(sender.Getchr)
    59.                End If
    60.            End If
    61.  
    62.        End If
    63.    End Sub

    Howdy folks.

    My little usercontrol has a problem (), it completely crashes when I try to use sender.GetChr.(the Getchr function will recieve and return values when its finished) Basically I have a sub which handles the click events for 10 AlphaNumericButton controls (top code is from that), and they have to retrieve a value using the Getchr function. Theres a timer which is shared across all of the usercontrols called keypadtimer (which is inside the control) which is set for 1 second. So the first time I click, the timer not enabled part fires and works properly. If I wait longer than 1 second, it'll work fine again... but if I click before 1 second has elapsed, I get the following error on the Msgbox part,


    An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll

    Additional information: Object reference not set to an instance of an object.



    Anyone?
    Last edited by Pc_Madness; May 17th, 2004 at 05:12 AM.
    Don't Rate my posts.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It sounds like sender or something else is not set to anything. You should test if sender is Nothing before trying to use it. It's also a good idea to cast it to a strong type instead of using the generic object type especially since object doesn't have a name property. Depending on what it is it may not have a GetChr method either.

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Use the Debug "Solution Configuration" and then run it from the IDE. What line do you get the error on?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Originally posted by Edneeis
    It sounds like sender or something else is not set to anything. You should test if sender is Nothing before trying to use it. It's also a good idea to cast it to a strong type instead of using the generic object type especially since object doesn't have a name property. Depending on what it is it may not have a GetChr method either.
    Well, I tried to do If sender <> Nothing Then and it had a spaz about that, but after it highlighted the error, sender wasn't empty, and since that part of the code will only occur when the error posted above would occur, I think we can assume that it isn't empty.

    And Jamie, the error is on the Msgbox (the second one) line in the btnKey2to9Handler sub.
    Don't Rate my posts.

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Try this :

    VB Code:
    1. Private Sub btnKey2to9Handler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKey2.Click, btnKey3.Click
    2. [b]    MsgBox(sender Is Nothing)[/b]
    3.  
    4.        If armed = True Or expectingInput = True Then
    5.            Static KeyValue As String
    6.            If Not lastKeyPressed Is sender Then
    7.                AlphaNumericButton.keyPadTimer.Enabled = False
    8.            End If
    9.            lastKeyPressed = sender
    10.  
    11.            If AlphaNumericButton.IsAlphabetic = False Then
    12.                KeyValue = sender.Getchr
    13.            Else
    14.  
    15.               'Working with this part
    16.                If Not AlphaNumericButton.keyPadTimer.Enabled Then
    17.                    KeyValue = sender.Getchr
    18.                    AlphaNumericButton.keyPadTimer.Enabled = True
    19.                Else
    20.                    'Delete the last alpha character.
    21.                    If inputBuffer.Length > 0 Then
    22.                        inputBuffer = inputBuffer.Substring(0, inputBuffer.Length - 1)
    23.                    End If
    24.                    'Determine the next alpha character.
    25.  
    26.                    'Errors here if I click within one second
    27. [b]    MsgBox(sender Is Nothing)[/b]
    28.                    MsgBox(sender.Getchr)
    29.                End If
    30.            End If
    31.  
    32.        End If
    33.    End Sub

    If it says false, then the problem might lie in an unhandled exception in the Getchr() method.
    I have never used UserControls, but there is the possibility that unhandled exceptions in UserControls are passed up through the call stack - instead of VB catching and showing you the error there.

    Try using MsgBox() statements in the Getchr() method to track its progress...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    The Msgbox's return false, and it seems it hates the two loops inside the GetChr() function.
    Don't Rate my posts.

  7. #7

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Woohoo, got it! Thanks Jamie. I shall name my least liked child in your honour.



    Don't Rate my posts.

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Let me guess - you never ReDimmed your tmpArray array ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Well, that and you can't use substring(0, 1), but I've cheated and just set it to array to 4 items and use mid instead due to substring's oddity (even when I did i + 1 )


    Don't Rate my posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width