2 Replies - 8119 Views - Last Post: 12 November 2011 - 02:35 PM Rate Topic: -----

#1 bluegalz02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-March 11

how to fix index out of range exception

Posted 12 November 2011 - 02:11 PM

i keep getting out of range exception when i run my code in vb..
it says that
HslSubj = Struktur(PosKt - 1)
is out of range exception unhandled..
but if i delete that i get a new error, which is argument exception.. please help me fix it..
i put my whole code below..

Public Class FrMain
    Public KDasar(0 To 9, 0 To 1) As String
    Public Imbuh(0 To 4, 0 To 1) As String
    Public Struktur(0 To 3) As String
    Public DesStruktur(0 To 3) As String

    Private Class KataDasar
        Private pVal As String, pValint As Integer
        Public Property description() As String
            Get
                Return pVal
            End Get
            Set(ByVal value As String)
                pVal = value
            End Set
        End Property
        Public Property groupID() As Integer
            Get
                Return pValint
            End Get
            Set(ByVal value As Integer)
                pValint = value
            End Set
        End Property
    End Class

    Private Class StrukKalimat
        Private pVal As String, pVal2 As Integer
        Public Property teks() As String
            Get
                Return pVal
            End Get
            Set(ByVal value As String)
                pVal = value
            End Set
        End Property
        Public Property IDstring() As String
            Get
                Return pVal2
            End Get
            Set(ByVal value As String)
                pVal2 = value
            End Set
        End Property
    End Class

    Sub InisialisasiDB()
        Imbuh(0, 0) = "men"
        Imbuh(1, 0) = "kan"
        Imbuh(2, 0) = "mem"
        Imbuh(3, 0) = "di"
        Imbuh(4, 0) = "ber"

        Imbuh(0, 1) = "0"
        Imbuh(1, 1) = "1"
        Imbuh(2, 1) = "0"
        Imbuh(3, 1) = "0"
        Imbuh(4, 1) = "0"

        KDasar(0, 0) = "dengar"
        KDasar(1, 0) = "simpan"
        KDasar(2, 0) = "belanja"
        KDasar(3, 0) = "baca"

        KDasar(0, 1) = "1"
        KDasar(1, 1) = "1"
        KDasar(2, 1) = "1"
        KDasar(3, 1) = "1"

        KDasar(4, 0) = "sekarang"
        KDasar(5, 0) = "dipasar"
        KDasar(6, 0) = "hariini"
        KDasar(7, 0) = "dilemari"
        KDasar(8, 0) = "siangini"

        KDasar(4, 1) = "3"
        KDasar(5, 1) = "3"
        KDasar(6, 1) = "3"
        KDasar(7, 1) = "3"
        KDasar(8, 1) = "3"

        KDasar(9, 0) = "mainan"
        KDasar(9, 1) = "0"

        Struktur(0) = "Subyek"
        Struktur(1) = "Predikat"
        Struktur(2) = "Objek"
        Struktur(3) = "Keterangan"

        DesStruktur(0) = "Subjek berupa kata benda (nomina)"
        DesStruktur(1) = "Predikat berupa kata kerja (verba)"
        DesStruktur(2) = "Objek berupa kata benda (nomina)"
        DesStruktur(3) = "Keterangan berupa keterangan waktu atau tempat"

    End Sub

    Function StateMachine1(ByVal kal As String) As String
        Dim HslString As String, HslKet As String, HslPred As String, HslSubj As String
        Dim HslObjek As String, hslImbuhan As String
        Dim pch() As String = Split(kal, " ")
        Dim A As Integer, e As Integer = -1, u As Integer, k As Integer, PosKt As Integer
        Dim PosKt1 As Integer

        For u = 0 To UBound(pch)
            For A = 0 To UBound(Imbuh)
                e = InStr(1, pch(u), Imbuh(A, 0), CompareMethod.Text)
                If (e > 0) And (e < Len(pch(u)) - 3) Then
                    If Imbuh(A, 1) = "0" Then hslImbuhan = hslImbuhan & Imbuh(A, 0) & "!"
                ElseIf e <> 0 Then
                    If e = Len(pch(u)) - (Len(Imbuh(A, 0)) + 1) Then
                        If Imbuh(A, 1) = "1" Then hslImbuhan = hslImbuhan & Imbuh(A, 0) & "!"
                    End If
                End If
            Next A
        Next u

        For k = 1 To 2
            For u = 0 To UBound(pch)
                For A = 0 To UBound(KDasar)
                    e = InStr(1, pch(u), KDasar(A, 0), CompareMethod.Text)
                    If e > 0 Then
                        If KDasar(A, 1) = "3" Then
                            HslKet = Struktur(CInt(KDasar(A, 1)))
                            PosKt1 = u
                        ElseIf KDasar(A, 1) = "1" Then
                            HslPred = Struktur(CInt(KDasar(A, 1)))
                            PosKt = u
                        End If
                    End If
                Next A
            Next u
        Next k

Lanjut:
        If PosKt1 = 0 Then
            PosKt = PosKt - 1
        End If

        HslObjek = Struktur(PosKt + 1)
        HslSubj = Struktur(PosKt - 1)
        If PosKt1 = 0 Then
            HslString = HslKet & "-" & HslSubj & "-" & HslPred & "-" & HslObjek & "#" & Mid(hslImbuhan, 1, Len(hslImbuhan) - 1)
        Else
            HslString = HslSubj & "-" & HslPred & "-" & HslObjek & "-" & HslKet & "#" & Mid(hslImbuhan, 1, Len(hslImbuhan) - 1)
        End If
        StateMachine1 = HslString
    End Function

    Function BaseParsing(ByVal kt As String) As String
        Dim A As Integer, e As Integer = 0
        BaseParsing = kt
        For A = 0 To UBound(KDasar)
            e = InStr(1, kt, KDasar(A, 0), CompareMethod.Text)
            If e > 0 Then
                BaseParsing = KDasar(A, 0)
                Exit For
            End If
        Next A
    End Function

    Sub StateMachine2()
        Dim Wparts() As String = Split(txinput.Text, " ")

        Dim state As Integer = 0
        While True
            '	        command = next command
            Select Case (state)
                Case 0
                    If (Command.Equals("USER")) Then
                        '       				username = argument;
                        state = 1
                    ElseIf (Command.Equals("QUIT")) Then
                        state = 4
                    Else
                        Error ("Unknown: " + Command())
                        '                      break()
                    End If
                Case 1
                    If (Command.Equals("PASS")) Then
                        '                       If (valid(username, argument)) Then
                        '                      state = 2
                        '                  Else
                        '                      Error ("Unauthorized")
                        '                      state = 3
                        '                   End If
                    Else
                        Error ("Unknown: " + Command())
                        Exit While
                    End If
            End Select
        End While
    End Sub

    Public Class SingleObjClass
        '	private  SingleObjClass obj = new SingleObjClass()
        Private SingleObjClass()
        ' 	public  SingleObjClass.getInstance()
        '	return obj;
    End Class

    Private Sub FrMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call InisialisasiDB()
        LS1A.Items.Clear()
        LS1B.Items.Clear()
        LS2.Items.Clear()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tmpRes As String = StateMachine1(LCase(txinput.Text))
        Dim chnk1() As String = Split(tmpRes, "#")
        Dim chnk2() As String = Split(chnk1(0), "-")
        Dim chnk3() As String = Split(chnk1(1), "!"), j As Integer

        Dim ptg() As String = Split(LCase(txinput.Text), " ")

        LS1A.Items.Clear()
        LS1B.Items.Clear()
        LS2.Items.Clear()

        For j = 0 To UBound(chnk2)
            LS2.Items.Add(chnk2(j))
        Next

        For j = 0 To UBound(chnk3)
            LS1B.Items.Add(chnk3(j))
        Next

        For j = 0 To UBound(ptg)
            LS1A.Items.Add(BaseParsing(ptg(j)))
        Next

    End Sub

    Private Sub LS2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LS2.SelectedIndexChanged
        Dim j As Integer
        For j = 0 To UBound(Struktur)
            If LS2.Items(LS2.SelectedIndex) = Struktur(j) Then
                LSDP.Text = DesStruktur(j)
                Exit For
            End If
        Next j
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        txinput.Text = ""
        LS1A.Items.Clear()
        LS1B.Items.Clear()
        LS2.Items.Clear()
        LSDP.Clear()
    End Sub
End Class


please help me fix it..
thank you so much..

Is This A Good Question/Topic? 0
  • +

Replies To: how to fix index out of range exception

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: how to fix index out of range exception

Posted 12 November 2011 - 02:26 PM

Look at this peace of code here:
 If PosKt1 = 0 Then
   PosKt = PosKt - 1 'you set it to -1
 End If

HslObjek = Struktur(PosKt + 1)
HslSubj = Struktur(PosKt - 1)'now you try to get -2
you are trying to set PosKt = PosKt - 1 but if PosKt was zero then you set it to -1, this will throw array ot of bounds because there is no -1 index. At line 101 you declare PosKt and it defaults to 0, so probably you are trying to get -2 index value
Was This Post Helpful? 0
  • +
  • -

#3 bluegalz02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-March 11

Re: how to fix index out of range exception

Posted 12 November 2011 - 02:35 PM

if I change into
If PosKt1 = 0 Then
            PosKt = PosKt + 1
        End If

        HslObjek = Struktur(PosKt + 1)
        HslSubj = Struktur(PosKt - 1)

then got another error which is argument exception on
 HslString = HslKet & "-" & HslSubj & "-" & HslPred & "-" & HslObjek & "#" & Mid(hslImbuhan, 1, Len(hslImbuhan) - 1)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1