11 Replies - 731 Views - Last Post: 17 February 2012 - 09:32 AM Rate Topic: -----

#1 pandep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 12

InvalidCast Exception was unhandled

Posted 15 February 2012 - 11:41 AM

'Pankaj Pande
'IS 622
'AVL Tree Assignment
'
'
'
Imports System
Public Class clsAVLTree

    'Data Members
    Private nRoot As clsNode 'Stores the root node of the tree

    'Constructor(s)
    Public Sub New()
        nRoot = Nothing
    End Sub

    'Properties 
    Public Property nodRoot() As clsNode
        Get
            Return nRoot
        End Get
        Set(ByVal nodValue As clsNode)
            nRoot = nodValue
        End Set
    End Property

    Property nodleft As clsNode

    Property nodright As clsNode

    'Methods
    Public Function intFindMin() As Integer 'Finding Minimum value
        Dim objCurrent As clsNode = nRoot
        While (Not (objCurrent.Left Is Nothing))
            objCurrent = objCurrent.Left
        End While
        objCurrent.DisplayNode()
        Return objCurrent.Data
    End Function

    Public Function intFindMax() As Integer 'Finding Maximum value
        Dim objCurrent As clsNode = nRoot
        While (Not (objCurrent.Right Is Nothing))
            objCurrent = objCurrent.Right
        End While
        objCurrent.DisplayNode()
        Return objCurrent.Data
    End Function

    Public Function Find(ByVal intKey As Integer) As clsNode 'Finding certain value
        Dim objCurrent As clsNode = nRoot
        While (objCurrent.Data <> intKey)
            If (intKey < objCurrent.Data) Then
                objCurrent = objCurrent.Left
            Else
                objCurrent = objCurrent.Right
            End If
            If (objCurrent Is Nothing) Then
                Console.WriteLine("Not in the set")
                Return Nothing
            End If
        End While
        objCurrent.DisplayNode()
        Console.WriteLine("Is in the set")
        Return objCurrent
    End Function

    'Inorder Traversal
    Public Sub inOrder(ByVal theRoot As clsNode)
        If (Not (theRoot Is Nothing)) Then
            inOrder(theRoot.Left)
            If theRoot.Deleted = False Then
                theRoot.DisplayNode()
            End If
            inOrder(theRoot.Right)
        End If
    End Sub

    'Breadth First Search Traversal or By Level Traversal
    Public Sub ByLevel(ByVal theRoot As clsNode, ByVal udtHead As clsNode, ByVal udtTail As clsNode)
        Dim queue As New clsQueue
        'Insert the root in the queue
        queue.AddTail(25, theRoot)
        recursiveSearch(theRoot, queue)
    End Sub

    Public Sub recursiveSearch(ByVal theRoot As clsNode, ByVal queue As clsQueue)
        theRoot.DisplayNode()
        queue.removeQueue()

        'Insert Left node
        If Not (theRoot.Left Is Nothing) Then
            queue.AddTail(23, theRoot.Left)
        End If

        'Insert Right node
        If Not (theRoot.Right Is Nothing) Then
            queue.AddTail(60, theRoot.Right)
        End If

        If Not (queue.Head.Right Is Nothing) Then
            Dim udtTemp As New Object
            udtTemp = queue.Head.Right.Data

            recursiveSearch(udtTemp, queue)
        End If
    End Sub

    'Insert Function
    Public Function Insert(ByVal objItem As Object, ByVal nodN As clsNode) As clsNode
        If (nodN Is Nothing) Then
            nodN = New clsNode(objItem, Nothing, Nothing)
        ElseIf (objItem.compareto(nodN.Data) < 0) Then
            nodN.Left = Insert(objItem, nodN.Left)
            If getHeight(nodN.Left) - getHeight(nodN.Right) = 2 Then
                If (objItem.CompareTo(nodN.Left.Data) < 0) Then
                    nodN = RotateWithLeftChild(nodN)
                Else
                    nodN = DoubleWithLeftChild(nodN)
                End If
            End If
        ElseIf (objItem.compareto(nodN.Data) > 0) Then
            nodN.Right = Insert(objItem, nodN.Right)
            If (getHeight(nodN.Right) - getHeight(nodN.Left) = 2) Then
                If (objItem.compareto(nodN.Right.Data) > 0) Then
                    nodN = RotateWithRightChild(nodN)
                Else
                    nodN = DoubleWithRightChild(nodN)
                End If
            End If
        Else
            'do nothing, duplicate value
        End If
        nodN.getHeight = Math.Max(getHeight(nodN.Left), getHeight(nodN.Right)) + 1
        Return nodN
    End Function

    Private Function RotateWithLeftChild(ByVal nodTwo As clsNode) As clsNode
        Dim nodOne As clsNode = nodTwo.Left
        nodTwo.Left = nodOne.Right
        nodOne.Right = nodTwo
        nodTwo.getHeight = Math.Max(getHeight(nodTwo.Left), getHeight(nodTwo.Right)) + 1
        nodOne.getHeight = Math.Max(getHeight(nodOne.Left), nodTwo.getHeight) + 1
        Return nodOne
    End Function

    Private Function RotateWithRightChild(ByVal nodOne As clsNode) As clsNode
        Dim nodTwo As clsNode = nodOne.Right
        nodOne.Right = nodTwo.Left
        nodTwo.Left = nodOne
        nodOne.getHeight = Math.Max(getHeight(nodOne.Left), getHeight(nodOne.Right)) + 1
        nodTwo.getHeight = Math.Max(getHeight(nodTwo.Right), nodOne.getHeight) + 1
        Return nodTwo
    End Function

    Private Function DoubleWithLeftChild(ByVal nodTwo As clsNode) As clsNode
        nodTwo.Left = RotateWithRightChild(nodTwo.Left)
        Return RotateWithLeftChild(nodTwo)
    End Function

    Private Function DoubleWithRightChild(ByVal nodTwo As clsNode) As clsNode
        nodTwo.Right = RotateWithLeftChild(nodTwo.Right)
        Return RotateWithRightChild(nodTwo)
    End Function

    Function getHeight(ByVal nodNode As clsNode) As Integer
        If nodNode Is Nothing Then
            Return 0
        Else
            Return nodNode.getHeight
        End If
    End Function

    Public Function getSuccessor(ByVal delNode As clsNode) As clsNode
        Dim nSuccessorParent As clsNode = delNode
        Dim nSuccessor As clsNode = delNode
        Dim nCurrent As clsNode = delNode.Right
        While (Not (nCurrent Is Nothing))
            nSuccessorParent.Left = nSuccessor
            nSuccessor = nCurrent
            nCurrent = nCurrent.Left
        End While
        If (Not (nSuccessor Is delNode.Right)) Then
            nSuccessorParent.Left = nSuccessor.Right
            nSuccessor.Right = delNode.Right
        End If
        Return nSuccessor
    End Function

    'Delete Function
    Public Function Delete(ByVal intD As Object, ByVal nodRoot As clsNode) As Boolean
        If nodRoot Is Nothing Then
            Return False
        Else
            If intD = nodRoot.Data Then
                nodRoot.Deleted = True
                Return True
            Else
                If Delete(intD, nodRoot.Left) = True Or Delete(intD, nodRoot.Right) = True Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If

    End Function
End Class

This post has been edited by smohd: 15 February 2012 - 11:47 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: InvalidCast Exception was unhandled

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 11:55 AM

on what line do you get the error on

This post has been edited by m_wylie85: 15 February 2012 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 pandep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 12

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 12:10 PM

View Postm_wylie85, on 15 February 2012 - 01:55 PM, said:

on what line do you get the error on


This is where I am getting an error message:
recursiveSearch(udtTemp, queue)
It is in the Public Sub recursiveSearch(ByVal theRoot As clsNode, ByVal queue As clsQueue) method
Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 12:11 PM

you should wrap your code around Try...Catch blocks so that you can find out the exceptions thrown from the program.

Public Sub testProgram()

        Try
            'the code goes here
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

End Sub

This post has been edited by nK0de: 15 February 2012 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 pandep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 12

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 12:19 PM

I am new to this VB.net and I am having hard time figuring out this problem. Can you please help me because I already tried so many things and nothing works.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 12:34 PM

Just something you can try that might help you find the source is to turn on Option Strict at the top of your class and see what errors it throws. I would say you are performing some kind of late binding that can't be cast to that object.

Just add Option Strict On as the first line of the code you have (Prior any Class)

This post has been edited by CharlieMay: 15 February 2012 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 01:32 PM

Could it be you're trying to cast an Object to a clsNode?
RecursiveSearch(udtTemp, queue)

Sorry, I'm just throwing out a suggestion.
Was This Post Helpful? 0
  • +
  • -

#8 pandep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 12

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 02:14 PM

View PostCharlieMay, on 15 February 2012 - 02:34 PM, said:

Just something you can try that might help you find the source is to turn on Option Strict at the top of your class and see what errors it throws. I would say you are performing some kind of late binding that can't be cast to that object.

Just add Option Strict On as the first line of the code you have (Prior any Class)



I tried adding 'Option Strict On' then it suggest me to change this line
RecursiveSearch(udtTemp, queue)
to this
recursiveSearch(CType(udtTemp, clsNode), queue)
but I'm still getting an error message in the queue. The same error I was getting before.
Was This Post Helpful? 0
  • +
  • -

#9 DeadJim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 05-November 09

Re: InvalidCast Exception was unhandled

Posted 15 February 2012 - 03:39 PM

(shot in the dark - hard to recreate the error with the clsQueue and clsNode class files)


Within the Subroutine 'recursiveSearch' you are defining udtTemp as an Object, then passing it back into a subsequent itteration of 'recursiveSearch'.

Try this:

Change line 103 -
From: Dim udtTemp As New Object

To: Dim udtTemp as New clsNode


See if that clears things up for you.

I also recommend heavy Breakpoint usage - place a breakpoint on line 88. See if the error is as a result of the very first call to the recursive function, or if it's a subsequent call.

-Cheers
Was This Post Helpful? 0
  • +
  • -

#10 pandep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 12

Re: InvalidCast Exception was unhandled

Posted 16 February 2012 - 10:10 AM

View PostDeadJim, on 15 February 2012 - 05:39 PM, said:

(shot in the dark - hard to recreate the error with the clsQueue and clsNode class files)


Within the Subroutine 'recursiveSearch' you are defining udtTemp as an Object, then passing it back into a subsequent itteration of 'recursiveSearch'.

Try this:

Change line 103 -
From: Dim udtTemp As New Object

To: Dim udtTemp as New clsNode


See if that clears things up for you.

I also recommend heavy Breakpoint usage - place a breakpoint on line 88. See if the error is as a result of the very first call to the recursive function, or if it's a subsequent call.

-Cheers


I did as you said but no luck. Now I am getting error on the line above the previous one. This is the line I am getting error.
udtTemp = CType(queue.Head.Right.Data, clsNode) - line 106

I also attached all my files if anyone can help me with this because I am totally lost. I will greatly appreciate any sort of help.
'clsAVLTree.vb

'Option Strict On

Public Class clsAVLTree

    'Data Members
    Private nRoot As clsNode 'Stores the root node of the tree

    'Constructor(s)
    Public Sub New()
        nRoot = Nothing
    End Sub

    'Properties 
    Public Property nodRoot() As clsNode
        Get
            Return nRoot
        End Get
        Set(ByVal nodValue As clsNode)
            nRoot = nodValue
        End Set
    End Property

    Property nodleft As clsNode

    Property nodright As clsNode

    'Methods
    Public Function intFindMin() As Integer 'Finding Minimum value
        Dim objCurrent As clsNode = nRoot
        While (Not (objCurrent.Left Is Nothing))
            objCurrent = objCurrent.Left
        End While
        objCurrent.DisplayNode()
        Return CInt(objCurrent.Data)
    End Function

    Public Function intFindMax() As Integer 'Finding Maximum value
        Dim objCurrent As clsNode = nRoot
        While (Not (objCurrent.Right Is Nothing))
            objCurrent = objCurrent.Right
        End While
        objCurrent.DisplayNode()
        Return CInt(objCurrent.Data)
    End Function

    Public Function Find(ByVal intKey As Integer) As clsNode 'Finding certain value
        Dim objCurrent As clsNode = nRoot
        While (objCurrent.Data <> intKey)
            If (intKey < objCurrent.Data) Then
                objCurrent = objCurrent.Left
            Else
                objCurrent = objCurrent.Right
            End If
            If (objCurrent Is Nothing) Then
                Console.WriteLine("Not in the set")
                Return Nothing
            End If
        End While
        objCurrent.DisplayNode()
        Console.WriteLine("Is in the set")
        Return objCurrent
    End Function

    'Inorder Traversal
    Public Sub inOrder(ByVal theRoot As clsNode)
        If (Not (theRoot Is Nothing)) Then
            inOrder(theRoot.Left)
            If theRoot.Deleted = False Then
                theRoot.DisplayNode()
            End If
            inOrder(theRoot.Right)
        End If
    End Sub

    'Breadth First Search Traversal or By Level Traversal
    Public Sub ByLevel(ByVal theRoot As clsNode, ByVal udtHead As clsNode, ByVal udtTail As clsNode)
        Dim queue As New clsQueue
        'Insert the root in the queue
        queue.AddTail(25, theRoot)
        recursiveSearch(CType(theRoot, clsNode), queue)
    End Sub

    Public Sub recursiveSearch(ByVal theRoot As clsNode, ByVal queue As clsQueue)
        theRoot.DisplayNode()
        queue.removeQueue()

        'Insert Left node
        If Not (theRoot.Left Is Nothing) Then
            queue.AddTail(23, theRoot.Left)
        End If

        'Insert Right node
        If Not (theRoot.Right Is Nothing) Then
            queue.AddTail(60, theRoot.Right)
        End If

        If Not (queue.Head.Right Is Nothing) Then
            Dim udtTemp As New clsNode
            [color="#FF0000"]udtTemp = CType(queue.Head.Right.Data, clsNode)[/color]

            recursiveSearch(CType(udtTemp, clsNode), queue)
        End If

    End Sub

    'Insert Function
    Public Function Insert(ByVal objItem As Object, ByVal nodN As clsNode) As clsNode
        If (nodN Is Nothing) Then
            nodN = New clsNode(CInt(objItem), Nothing, Nothing)
        ElseIf (objItem.compareto(nodN.Data) < 0) Then
            nodN.Left = Insert(objItem, nodN.Left)
            If getHeight(nodN.Left) - getHeight(nodN.Right) = 2 Then
                If (objItem.CompareTo(nodN.Left.Data) < 0) Then
                    nodN = RotateWithLeftChild(nodN)
                Else
                    nodN = DoubleWithLeftChild(nodN)
                End If
            End If
        ElseIf (objItem.compareto(nodN.Data) > 0) Then
            nodN.Right = Insert(objItem, nodN.Right)
            If (getHeight(nodN.Right) - getHeight(nodN.Left) = 2) Then
                If (objItem.compareto(nodN.Right.Data) > 0) Then
                    nodN = RotateWithRightChild(nodN)
                Else
                    nodN = DoubleWithRightChild(nodN)
                End If
            End If
        Else
            'do nothing, duplicate value
        End If
        nodN.getHeight = Math.Max(getHeight(nodN.Left), getHeight(nodN.Right)) + 1
        Return nodN
    End Function

    Private Function RotateWithLeftChild(ByVal nodTwo As clsNode) As clsNode
        Dim nodOne As clsNode = nodTwo.Left
        nodTwo.Left = nodOne.Right
        nodOne.Right = nodTwo
        nodTwo.getHeight = Math.Max(getHeight(nodTwo.Left), getHeight(nodTwo.Right)) + 1
        nodOne.getHeight = Math.Max(getHeight(nodOne.Left), nodTwo.getHeight) + 1
        Return nodOne
    End Function

    Private Function RotateWithRightChild(ByVal nodOne As clsNode) As clsNode
        Dim nodTwo As clsNode = nodOne.Right
        nodOne.Right = nodTwo.Left
        nodTwo.Left = nodOne
        nodOne.getHeight = Math.Max(getHeight(nodOne.Left), getHeight(nodOne.Right)) + 1
        nodTwo.getHeight = Math.Max(getHeight(nodTwo.Right), nodOne.getHeight) + 1
        Return nodTwo
    End Function

    Private Function DoubleWithLeftChild(ByVal nodTwo As clsNode) As clsNode
        nodTwo.Left = RotateWithRightChild(nodTwo.Left)
        Return RotateWithLeftChild(nodTwo)
    End Function

    Private Function DoubleWithRightChild(ByVal nodTwo As clsNode) As clsNode
        nodTwo.Right = RotateWithLeftChild(nodTwo.Right)
        Return RotateWithRightChild(nodTwo)
    End Function

    Function getHeight(ByVal nodNode As clsNode) As Integer
        If nodNode Is Nothing Then
            Return 0
        Else
            Return nodNode.getHeight
        End If
    End Function

    Public Function getSuccessor(ByVal delNode As clsNode) As clsNode
        Dim nSuccessorParent As clsNode = delNode
        Dim nSuccessor As clsNode = delNode
        Dim nCurrent As clsNode = delNode.Right
        While (Not (nCurrent Is Nothing))
            nSuccessorParent.Left = nSuccessor
            nSuccessor = nCurrent
            nCurrent = nCurrent.Left
        End While
        If (Not (nSuccessor Is delNode.Right)) Then
            nSuccessorParent.Left = nSuccessor.Right
            nSuccessor.Right = delNode.Right
        End If
        Return nSuccessor
    End Function

    'Delete Function
    Public Function Delete(ByVal intD As Object, ByVal nodRoot As clsNode) As Boolean
        If nodRoot Is Nothing Then
            Return False
        Else
            If intD = nodRoot.Data Then
                nodRoot.Deleted = True
                Return True
            Else
                If Delete(intD, nodRoot.Left) = True Or Delete(intD, nodRoot.Right) = True Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If

    End Function
End Class

Attached File(s)


This post has been edited by modi123_1: 16 February 2012 - 12:00 PM
Reason for edit:: please use code tags

Was This Post Helpful? 0
  • +
  • -

#11 DeadJim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 05-November 09

Re: InvalidCast Exception was unhandled

Posted 16 February 2012 - 12:15 PM

Good news - I got it to blow up. Even more good news, I got it to stop blowing up. Possibly Bad news would be that I really don't know if the app is doing what you want it to do, so I'm going to let you guage that.

My change was pretty simple:

 If Not (queue.Head.Right Is Nothing) Then
            Dim udtTemp As New clsNode

            udtTemp = queue.Head.Right 'Was queue.Head.Right.Data

            recursiveSearch(CType(udtTemp, clsNode), queue)
        End If



Drop the .Data value from your temp assignment - it resulted initially into an attempt to shove an Int value where a clsNode was expected. Casting this same Integer value as a clsNode was the compiler/IDE's attempt to set things right.

Good luck
Was This Post Helpful? 1
  • +
  • -

#12 pandep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 12

Re: InvalidCast Exception was unhandled

Posted 17 February 2012 - 09:32 AM

View PostDeadJim, on 16 February 2012 - 02:15 PM, said:

Good news - I got it to blow up. Even more good news, I got it to stop blowing up. Possibly Bad news would be that I really don't know if the app is doing what you want it to do, so I'm going to let you guage that.

My change was pretty simple:

 If Not (queue.Head.Right Is Nothing) Then
            Dim udtTemp As New clsNode

            udtTemp = queue.Head.Right 'Was queue.Head.Right.Data

            recursiveSearch(CType(udtTemp, clsNode), queue)
        End If



Drop the .Data value from your temp assignment - it resulted initially into an attempt to shove an Int value where a clsNode was expected. Casting this same Integer value as a clsNode was the compiler/IDE's attempt to set things right.

Good luck



I did as you have mentioned, and now I am getting Stack Overflow message.
StackOverflowException was unhandled
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
Make sure you do not have an infinite loop or infinite recursion.
Public Sub DisplayNode()
Console.Write(intData.ToString() & " ")
End Sub
This line is on the clsNode file.

Thank you very much for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1