473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fix out of range exception

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

Expand|Select|Wrap|Line Numbers
  1. Public Class FrMain
  2.     Public KDasar(0 To 9, 0 To 1) As String
  3.     Public Imbuh(0 To 4, 0 To 1) As String
  4.     Public Struktur(0 To 3) As String
  5.     Public DesStruktur(0 To 3) As String
  6.  
  7.     Private Class KataDasar
  8.         Private pVal As String, pValint As Integer
  9.         Public Property description() As String
  10.             Get
  11.                 Return pVal
  12.             End Get
  13.             Set(ByVal value As String)
  14.                 pVal = value
  15.             End Set
  16.         End Property
  17.         Public Property groupID() As Integer
  18.             Get
  19.                 Return pValint
  20.             End Get
  21.             Set(ByVal value As Integer)
  22.                 pValint = value
  23.             End Set
  24.         End Property
  25.     End Class
  26.  
  27.     Private Class StrukKalimat
  28.         Private pVal As String, pVal2 As Integer
  29.         Public Property teks() As String
  30.             Get
  31.                 Return pVal
  32.             End Get
  33.             Set(ByVal value As String)
  34.                 pVal = value
  35.             End Set
  36.         End Property
  37.         Public Property IDstring() As String
  38.             Get
  39.                 Return pVal2
  40.             End Get
  41.             Set(ByVal value As String)
  42.                 pVal2 = value
  43.             End Set
  44.         End Property
  45.     End Class
  46.  
  47.     Sub InisialisasiDB()
  48.         Imbuh(0, 0) = "men"
  49.         Imbuh(1, 0) = "kan"
  50.         Imbuh(2, 0) = "mem"
  51.         Imbuh(3, 0) = "di"
  52.         Imbuh(4, 0) = "ber"
  53.  
  54.         Imbuh(0, 1) = "0"
  55.         Imbuh(1, 1) = "1"
  56.         Imbuh(2, 1) = "0"
  57.         Imbuh(3, 1) = "0"
  58.         Imbuh(4, 1) = "0"
  59.  
  60.         KDasar(0, 0) = "dengar"
  61.         KDasar(1, 0) = "simpan"
  62.         KDasar(2, 0) = "belanja"
  63.         KDasar(3, 0) = "baca"
  64.  
  65.         KDasar(0, 1) = "1"
  66.         KDasar(1, 1) = "1"
  67.         KDasar(2, 1) = "1"
  68.         KDasar(3, 1) = "1"
  69.  
  70.         KDasar(4, 0) = "sekarang"
  71.         KDasar(5, 0) = "dipasar"
  72.         KDasar(6, 0) = "hariini"
  73.         KDasar(7, 0) = "dilemari"
  74.         KDasar(8, 0) = "siangini"
  75.  
  76.         KDasar(4, 1) = "3"
  77.         KDasar(5, 1) = "3"
  78.         KDasar(6, 1) = "3"
  79.         KDasar(7, 1) = "3"
  80.         KDasar(8, 1) = "3"
  81.  
  82.         KDasar(9, 0) = "mainan"
  83.         KDasar(9, 1) = "0"
  84.  
  85.         Struktur(0) = "Subyek"
  86.         Struktur(1) = "Predikat"
  87.         Struktur(2) = "Objek"
  88.         Struktur(3) = "Keterangan"
  89.  
  90.         DesStruktur(0) = "Subjek berupa kata benda (nomina)"
  91.         DesStruktur(1) = "Predikat berupa kata kerja (verba)"
  92.         DesStruktur(2) = "Objek berupa kata benda (nomina)"
  93.         DesStruktur(3) = "Keterangan berupa keterangan waktu atau tempat"
  94.  
  95.     End Sub
  96.  
  97.     Function StateMachine1(ByVal kal As String) As String
  98.         Dim HslString As String, HslKet As String, HslPred As String, HslSubj As String
  99.         Dim HslObjek As String, hslImbuhan As String
  100.         Dim pch() As String = Split(kal, " ")
  101.         Dim A As Integer, e As Integer = -1, u As Integer, k As Integer, PosKt As Integer
  102.         Dim PosKt1 As Integer
  103.  
  104.         For u = 0 To UBound(pch)
  105.             For A = 0 To UBound(Imbuh)
  106.                 e = InStr(1, pch(u), Imbuh(A, 0), CompareMethod.Text)
  107.                 If (e > 0) And (e < Len(pch(u)) - 3) Then
  108.                     If Imbuh(A, 1) = "0" Then hslImbuhan = hslImbuhan & Imbuh(A, 0) & "!"
  109.                 ElseIf e <> 0 Then
  110.                     If e = Len(pch(u)) - (Len(Imbuh(A, 0)) + 1) Then
  111.                         If Imbuh(A, 1) = "1" Then hslImbuhan = hslImbuhan & Imbuh(A, 0) & "!"
  112.                     End If
  113.                 End If
  114.             Next A
  115.         Next u
  116.  
  117.         For k = 1 To 2
  118.             For u = 0 To UBound(pch)
  119.                 For A = 0 To UBound(KDasar)
  120.                     e = InStr(1, pch(u), KDasar(A, 0), CompareMethod.Text)
  121.                     If e > 0 Then
  122.                         If KDasar(A, 1) = "3" Then
  123.                             HslKet = Struktur(CInt(KDasar(A, 1)))
  124.                             PosKt1 = u
  125.                         ElseIf KDasar(A, 1) = "1" Then
  126.                             HslPred = Struktur(CInt(KDasar(A, 1)))
  127.                             PosKt = u
  128.                         End If
  129.                     End If
  130.                 Next A
  131.             Next u
  132.         Next k
  133.  
  134. Lanjut:
  135.         If PosKt1 = 0 Then
  136.             PosKt = PosKt - 1
  137.         End If
  138.  
  139.         HslObjek = Struktur(PosKt + 1)
  140.         HslSubj = Struktur(PosKt - 1)
  141.         If PosKt1 = 0 Then
  142.             HslString = HslKet & "-" & HslSubj & "-" & HslPred & "-" & HslObjek & "#" & Mid(hslImbuhan, 1, Len(hslImbuhan) - 1)
  143.         Else
  144.             HslString = HslSubj & "-" & HslPred & "-" & HslObjek & "-" & HslKet & "#" & Mid(hslImbuhan, 1, Len(hslImbuhan) - 1)
  145.         End If
  146.         StateMachine1 = HslString
  147.     End Function
  148.  
  149.     Function BaseParsing(ByVal kt As String) As String
  150.         Dim A As Integer, e As Integer = 0
  151.         BaseParsing = kt
  152.         For A = 0 To UBound(KDasar)
  153.             e = InStr(1, kt, KDasar(A, 0), CompareMethod.Text)
  154.             If e > 0 Then
  155.                 BaseParsing = KDasar(A, 0)
  156.                 Exit For
  157.             End If
  158.         Next A
  159.     End Function
  160.  
  161.     Sub StateMachine2()
  162.         Dim Wparts() As String = Split(txinput.Text, " ")
  163.  
  164.         Dim state As Integer = 0
  165.         While True
  166.             '            command = next command
  167.             Select Case (state)
  168.                 Case 0
  169.                     If (Command.Equals("USER")) Then
  170.                         '                       username = argument;
  171.                         state = 1
  172.                     ElseIf (Command.Equals("QUIT")) Then
  173.                         state = 4
  174.                     Else
  175.                         Error ("Unknown: " + Command())
  176.                         '                      break()
  177.                     End If
  178.                 Case 1
  179.                     If (Command.Equals("PASS")) Then
  180.                         '                       If (valid(username, argument)) Then
  181.                         '                      state = 2
  182.                         '                  Else
  183.                         '                      Error ("Unauthorized")
  184.                         '                      state = 3
  185.                         '                   End If
  186.                     Else
  187.                         Error ("Unknown: " + Command())
  188.                         Exit While
  189.                     End If
  190.             End Select
  191.         End While
  192.     End Sub
  193.  
  194.     Public Class SingleObjClass
  195.         '    private  SingleObjClass obj = new SingleObjClass()
  196.         Private SingleObjClass()
  197.         '     public  SingleObjClass.getInstance()
  198.         '    return obj;
  199.     End Class
  200.  
  201.     Private Sub FrMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  202.         Call InisialisasiDB()
  203.         LS1A.Items.Clear()
  204.         LS1B.Items.Clear()
  205.         LS2.Items.Clear()
  206.     End Sub
  207.  
  208.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  209.         Dim tmpRes As String = StateMachine1(LCase(txinput.Text))
  210.         Dim chnk1() As String = Split(tmpRes, "#")
  211.         Dim chnk2() As String = Split(chnk1(0), "-")
  212.         Dim chnk3() As String = Split(chnk1(1), "!"), j As Integer
  213.  
  214.         Dim ptg() As String = Split(LCase(txinput.Text), " ")
  215.  
  216.         LS1A.Items.Clear()
  217.         LS1B.Items.Clear()
  218.         LS2.Items.Clear()
  219.  
  220.         For j = 0 To UBound(chnk2)
  221.             LS2.Items.Add(chnk2(j))
  222.         Next
  223.  
  224.         For j = 0 To UBound(chnk3)
  225.             LS1B.Items.Add(chnk3(j))
  226.         Next
  227.  
  228.         For j = 0 To UBound(ptg)
  229.             LS1A.Items.Add(BaseParsing(ptg(j)))
  230.         Next
  231.  
  232.     End Sub
  233.  
  234.     Private Sub LS2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LS2.SelectedIndexChanged
  235.         Dim j As Integer
  236.         For j = 0 To UBound(Struktur)
  237.             If LS2.Items(LS2.SelectedIndex) = Struktur(j) Then
  238.                 LSDP.Text = DesStruktur(j)
  239.                 Exit For
  240.             End If
  241.         Next j
  242.     End Sub
  243.  
  244.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  245.         txinput.Text = ""
  246.         LS1A.Items.Clear()
  247.         LS1B.Items.Clear()
  248.         LS2.Items.Clear()
  249.         LSDP.Clear()
  250.     End Sub
  251. End Class
please help me fix it..
thank you so much..
Nov 12 '11 #1
1 2241
Rabbit
12,516 Expert Mod 8TB
In your code, there exists the possibility that the index you're attempting to access goes outside the range of 0-3.
Nov 14 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: smith flyers | last post by:
string thingword=" testing hmmm"; string firstLetter,restOfWord; foreach (string word in thingword.Split()) { Console.WriteLine(word); // if this only use for output and the rest in...
3
by: Pengyu | last post by:
Whenever I call listView.RemoveAt(listView.Count-1), which remove the last item, I get an out of range exception. How to solve this problem? Thanks a lot, Pengyu.
0
by: Bill Johnson | last post by:
I have a further question regarding an archive post at: ...
4
by: Brad | last post by:
Now I am encountering my second very strange problem. I will describe the first one later. But my current problem lies with trying to bind to a combobox: Try If clubID <> "" And txtYear.Text <>...
2
by: Henry J. | last post by:
Has anybody run into this index out range exception when opening and then closing a collectionEditor from within a PropertyGrid? I use PropertyGrid to edit configurations in my application. One...
0
by: Nick | last post by:
Hi people, I have a custom control combo box in a windows application that has a list of items to be displayed. there are currently 17 items that are bound to the table that acts as the data...
5
by: pcnerd | last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap & then displays the bitmap. When I run the program, I see the pixels being plotted down the left side of the form. When...
1
by: dedipya | last post by:
Environment W2003K with Oracle 10 client connected to oracle using oracle odp provider.orcle is on solaries machine. while filling the dataset .net is throughing the error "argument was out of the...
7
by: Pucca | last post by:
Hi: Below is the error I got from the 2 lines of code below. I don't understand why and how to correct it. The actionMenu.DropDownItems has 0 item in its collection at the time of the code. ...
2
by: GR M | last post by:
Hi, I was trying to add two quantities at different rows of same description i.e same product_name and same item_pack.1st I want to check the table if it contains the same description or not by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.