System.IndexOutOfRangeException error visual basic 2008

I am getting a System.IndexOutOfRangeException error when I run this in Visual Studio 2008 - can someone explain what I have done wrong in the code?


Option Strict On

Public Class frmFlicks

    'Class Level Private Variables
    Public Shared _intSizeOfArray As Integer = 17
    Public Shared _strInventoryItem(_intSizeOfArray) As String
    Private _strItemId(_intSizeOfArray) As String


    Private Sub frmFlicksPublish_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' The frmFlicksPublish load event reads the inventory text file and
        ' fills the listbox object with the inventory items.

        'Initialize an instance of the streamreader object and declare variables
        Dim objReader As IO.StreamReader
        Dim strLocationAndNameOfFile As String = "D:\Flicks\FlicksFictional\FlicksFictional\warehouse.txt"
        Dim intCount As Integer = 0
        'Dim intFill As Integer
        Dim strFileError As String = "The file is not available. Restart when the file is available."



        'Verify the file exists
        If IO.File.Exists(strLocationAndNameOfFile) = True Then
            objReader = IO.File.OpenText(strLocationAndNameOfFile)

            'Read the file line by line until the file is completed.

            Do While objReader.Peek <> -1
                _strInventoryItem(intCount) = objReader.ReadLine() 'this is where the error is
                _strItemId(intCount) = objReader.ReadLine()
                intCount += 1
            Loop
            objReader.Close()

            ' The ListBox object is filled with IDs
            For intFill = 0 To 16
                Me.lstBooks.Items.Add(_strItemId(intFill))
            Next
        Else
            MsgBox(strFileError, , "Error")
            Me.Close()
        End If
    End Sub

    Private Sub btnComputeInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeInventory.Click
        ' The btnComputeInventory click event calls the Compute sub procedures
        'Declare Variables
        Dim intTotalBooksOnHand As Integer
        Dim intTotalBooksOnOrder As Integer
        Dim decTotalCostBooksHand As Decimal
        Dim decTotalCostBooksOrder As Decimal


    End Sub
End Class
LVL 1
joylene6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:
What are the contents of text file, I tried a simple text file, but i am not getting error on the line you specified.
Also,

' The ListBox object is filled with IDs
            For intFill = 0 To 16
                Me.lstBooks.Items.Add(_strItemId(intFill))
            Next
Above code assumes that there will be 16 lines in text file. if rows are less then there might be error.
0
CodeCruiserCommented:
The  _strInventoryItem array is declared to be 17 items and the file is returning more lines. Here is an easier approach. Use following to read a text file as a string array

Dim FileLines As String() = IO.File.ReadAllLines("filepath")
0
joylene6Author Commented:
a sample txt file is attached.

I am trying to build 4 arrays with 17 elements.  The arrays need to bring back total books on hand,total on order,total cost of books on hand , and total cost of books on order. I have been working on this, and dont know why I get this error.
warehouse.txt
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

CodeCruiserCommented:
I wrote this code in Notepad for you. Try it out.



Dim Titles(17) as string
Dim Price(17) As Double
Dim Quantity1(17) as integer
Dim Quantity(17) as Integer


Dim Lines() As String = IO.File.ReadAllLines
Dim J as integer=0
For i as integer = 0 to 65 step 4
    Titles(j) = Lines(i)
    J+=1
Next
j=0
For i as integer = 1 to 66 step 4
    Titles(j) = Lines(i)
    J+=1
Next
j=0
For i as integer = 2 to 67 step 4
    Titles(j) = Lines(i)
    J+=1
Next
j=0
For i as integer = 3 to 68 step 4
    Titles(j) = Lines(i)
    J+=1
Next

Open in new window

0
joylene6Author Commented:
CodeCruiser - Wow -Thanks.  Is this Visual Basic code?

I was thinking I needed something like this - see what you think - I want the  total books on hand,total on order,total cost of books on hand , and total cost of books on order to be attached to seperate labels on the form.  I have to use the values in my .txt file to make these appear in the form.

 - I dont know.. I admit I am very lost on this one.   I dont know if the 0,1,2,3 are right.



BooksOnHand.text = PresentInventory(0).TotalOnHand
TotalBooksOnOrder.text = PresentInventory(1).TotalOnOrder
TotalCostBooksHand.text = PresentInventory(2).TotalOnHand
TotalCostBooksOrder.text = PresentInventory(3).TotalOnHand


0
CodeCruiserCommented:
If you want the totals across the file then you need to use some more loops such as

Dim TotalQuantityOnHand, TotalQuantityOnOrder, TotalCostOnHand, TotalCostOnOrder as integer
For i as integer = 0 to 16
      TotalQuantityOnHand += Quantity1(i)
      TotalQuantityOnOrder += Quantity2(i)
      TotalCostOnHand += Quantity1(i) * Price(i)
      TotalCostOnOrder += Quantity2(i) * Price(i)
Next


This code assumes that Quantity1 is quantity on hand and Quantity2 is quantity on order.
0
joylene6Author Commented:
I am also putting in the code now and trying it
0
joylene6Author Commented:
I am really trying to understand these arrays.  I tried to put your suggestions in -  For the Quantity1 and 2 and Price. I get a " Expression is not an array or method and cannot have an argument list"



Option Strict On

Public Class frmFlicks

    'Class Level Private Variables
    Public Shared _intSizeOfArray As Integer = 17
    Public Shared _strInventoryItem(_intSizeOfArray) As String
    Private _strItemId(_intSizeOfArray) As String


    Private Sub frmFlicksPublish_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' The frmFlicksPublish load event reads the inventory text file and
        ' fills the listbox object with the inventory items.

        'Initialize an instance of the streamreader object and declare variables
        Dim objReader As IO.StreamReader
        Dim strLocationAndNameOfFile As String = "D:\Flicks\FlicksFictional\FlicksFictional\warehouse.txt"
        Dim intCount As Integer = 0
        'Dim intFill As Integer
        Dim strFileError As String = "The file is not available. Restart when the file is available."



        'Verify the file exists
        If IO.File.Exists(strLocationAndNameOfFile) = True Then
            objReader = IO.File.OpenText(strLocationAndNameOfFile)

            'Read the file line by line until the file is completed.

            Do While objReader.Peek <> -1
                _strInventoryItem(intCount) = objReader.ReadLine()
                _strItemId(intCount) = objReader.ReadLine()
                intCount += 1
            Loop
            objReader.Close()

            ' The ListBox object is filled with IDs
            For intFill = 0 To 16
                Me.lstBooks.Items.Add(_strItemId(intFill))
            Next
        Else
            MsgBox(strFileError, , "Error")
            Me.Close()
        End If
    End Sub

    Private Sub btnComputeInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeInventory.Click
        ' The btnComputeInventory click event calls the Compute sub procedures
        'Declare Variables

        Dim Quantity1 As Integer
        Dim Quantity2 As Integer
        Dim Price As Decimal
        Dim TotalQuantityOnHand As Integer
        Dim TotalQuantityOnOrder As Integer
        Dim TotalCostOnHand As Decimal
        Dim TotalCostOnOrder As Integer
        For i As Integer = 0 To 16
            TotalQuantityOnHand += Quantity1(i)
            TotalQuantityOnOrder += Quantity2(i)
            TotalCostOnHand += Quantity1(i) * Price(i)
            TotalCostOnOrder += Quantity2(i) * Price(i)
        Next



    End Sub
End Class


*************************************************************
here is the TXT file information I am dealing with:
Title of book, price, Books on hand, books on order.  I am to assign the text file contents to 4 arrays. Each array having 17 elements.   total books on hand,total on order,total cost of books on hand , and total cost of books on order

Forensics Under Glass
24.95
24500
0
Cyber Heist
32.99
16895
10000
Nerd by Night
18.99
1322
2000
By the Light of the Monitor
23.95
2214
0
The Yellow Wall Paper
12.95
145
1500
The November Release
19.99
3800
2300
Techo Thriller
39.99
12000
1500
Cyberkiss
22.59
289
0
The Grid
30.99
4534
1200
Terminal Hacker
16.99
2399
1000
Deadly App
35.99
11100
200
Raw Data
29.99
266
1500
Love Bytes
38.99
23000
44000
Cyber Chick
27.99
399
0
Internet Identities
42.99
2560
400
Rueful RAM
17.95
4000
16000
Technology Lies
29.95
12999
19990

0
CodeCruiserCommented:
If you refer back to the original code i posted for reading the file, quantity1, quantity2, and price are arrays.

Dim Titles(17) as string
Dim Price(17) As Double
Dim Quantity1(17) as integer
Dim Quantity2(17) as Integer
0
joylene6Author Commented:
DO you want me to open another question to help debug this thing?
I am having trouble getting the numbers to the form.



Public Class frmFlicks

    'Class Level Private Variables
    Public Shared _intSizeOfArray As Integer = 17
    Public Shared _strInventoryItem(_intSizeOfArray) As String
    Private _strItemId(_intSizeOfArray) As String
    Private _decBookPrice(_intSizeOfArray) As Decimal
    Private _intNumberOfBooks(_intSizeOfArray) As Integer
    Private _intNumberOnOrder(_intSizeOfArray) As Integer



    Private Sub frmFlicksPublish_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' The frmFlicksPublish load event reads the inventory text file

        'Initialize an instance of the streamreader object and declare variables

        Dim objReader As IO.StreamReader
        Dim strLocationAndNameOfFile As String = "D:\warehouse.txt"
        Dim intCount As Integer = 0
        'Dim intFill As Integer
        Dim strFileError As String = "The file is not available. Restart when the file is available."



        'Verify the file exists
        If IO.File.Exists(strLocationAndNameOfFile) = True Then
            objReader = IO.File.OpenText(strLocationAndNameOfFile)

            'Read the file line by line until the file is completed.

            Do While objReader.Peek <> -1
                _strInventoryItem(intCount) = objReader.ReadLine()
                _decBookPrice(_intSizeOfArray) = Convert.ToDecimal(objReader.ReadLine())
                _intNumberOfBooks(_intSizeOfArray) = Convert.ToInt32(objReader.ReadLine())
                _intNumberOnOrder(_intSizeOfArray) = Convert.ToInt32(objReader.ReadLine())
                intCount += 1
            Loop
            objReader.Close()

            ' The Combo ListBox object is filled with IDs
            For intFill = 0 To 16
                Me.lstBooks.Items.Add(_strItemId(intFill))
            Next
        Else
            MsgBox(strFileError, , "Error")
            Me.Close()
        End If

    End Sub



    Private Sub btnComputeInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeInventory.Click
        ' The btnComputeInventory click event calls the Compute sub procedures
        'Declare Variables

        Dim intTotalBooksOnHand As Integer
        Dim intTotalBooksOnOrder As Integer
        Dim decTotalCostBooksHand As Decimal
        Dim decTotalCostBooksOrder As Decimal

        ' This section is a mess

        Dim Titles(17) As String
        Dim Price(17) As Double
        Dim Quantity1(17) As Integer
        Dim Quantity2(17) As Integer
        Dim TotalQuantityOnHand As Integer
        Dim TotalQuantityOnOrder As Integer
        Dim TotalCostOnHand As Decimal
        Dim TotalCostOnOrder As Integer
        For i As Integer = 0 To 16
            TotalQuantityOnHand += Quantity1(i)
            TotalQuantityOnOrder += Quantity2(i)
            TotalCostOnHand += Quantity1(i) * Price(i)
            TotalCostOnOrder += Quantity2(i) * Price(i)

            Me.lblInventory = TotalQuantityOnHand  ' is this how I get the total books on hand to show on the form? I get an
            ' error that says Value of type Integer cannot be converted to Ststem.Windows.Forms.Label



        Next



    End Sub
End Class
0
CodeCruiserCommented:
The problem is that you are sticking to your own code for reading the arrays but using my code to sum up those arrays. Put all my code from comment 29461872 and command 29464250 in a test form and try again.
0
joylene6Author Commented:
OK - I added your code.  I am gettin an AgrumentNullExcetion - but maybe I was supposed to take this part out or something.

Public Class frmFlicks

    'Class Level Private Variables
    Public Shared _intSizeOfArray As Integer = 17
    Public Shared _strInventoryItem(_intSizeOfArray) As String
    Private _strItemId(_intSizeOfArray) As String
    Private _decBookPrice(_intSizeOfArray) As Decimal
    Private _intNumberOfBooks(_intSizeOfArray) As Integer
    Private _intNumberOnOrder(_intSizeOfArray) As Integer



    Private Sub frmFlicksPublish_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' The frmFlicksPublish load event reads the inventory text file

        'Initialize an instance of the streamreader object and declare variables

        Dim objReader As IO.StreamReader
        Dim strLocationAndNameOfFile As String = "E:\Flicks\FlicksFictional\FlicksFictional\warehouse.txt"
        Dim intCount As Integer = 0
        'Dim intFill As Integer
        Dim strFileError As String = "The file is not available. Restart when the file is available."



        'Verify the file exists
        If IO.File.Exists(strLocationAndNameOfFile) = True Then
            objReader = IO.File.OpenText(strLocationAndNameOfFile)

            'Read the file line by line until the file is completed.

            Do While objReader.Peek <> -1
                _strInventoryItem(intCount) = objReader.ReadLine()
                _decBookPrice(_intSizeOfArray) = Convert.ToDecimal(objReader.ReadLine())
                _intNumberOfBooks(_intSizeOfArray) = Convert.ToInt32(objReader.ReadLine())
                _intNumberOnOrder(_intSizeOfArray) = Convert.ToInt32(objReader.ReadLine())
                intCount += 1
            Loop
            objReader.Close()

            ' The Combo ListBox object is filled with IDs
            For intFill = 0 To 16
                Me.lstBooks.Items.Add(_strItemId(intFill)) '*** This is the ERROR line
            Next
        Else
            MsgBox(strFileError, , "Error")
            Me.Close()
        End If

    End Sub



    Private Sub btnComputeInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeInventory.Click
        ' The btnComputeInventory click event calls the Compute sub procedures
        'Declare Variables

        'Dim intTotalBooksOnHand As Integer
        'Dim intTotalBooksOnOrder As Integer
        'Dim decTotalCostBooksHand As Decimal
        'Dim decTotalCostBooksOrder As Decimal

        ' This section is a mess
        'Dim Titles(17) As String
        'Dim Price(17) As Double
        'Dim Quantity1(17) As Integer
        'Dim Quantity(17) As Integer


        Dim Titles(17) As String
        Dim Price(17) As Double
        Dim Quantity1(17) As Integer
        Dim Quantity2(17) As Integer


        Dim Lines() As String = IO.File.ReadAllLines("E:\Flicks\FlicksFictional\FlicksFictional\warehouse.txt")
        Dim J As Integer = 0
        For i As Integer = 0 To 65 Step 4
            Titles(J) = Lines(i)
            J += 1
        Next
        J = 0
        For i As Integer = 1 To 66 Step 4
            Titles(J) = Lines(i)
            J += 1
        Next
        J = 0
        For i As Integer = 2 To 67 Step 4
            Titles(J) = Lines(i)
            J += 1
        Next
        J = 0
        For i As Integer = 3 To 68 Step 4
            Titles(J) = Lines(i)
            J += 1
        Next
       
        Dim TotalQuantityOnHand, TotalQuantityOnOrder, TotalCostOnHand, TotalCostOnOrder As Integer
        For i As Integer = 0 To 16
            TotalQuantityOnHand += Quantity1(i)
            TotalQuantityOnOrder += Quantity2(i)
            TotalCostOnHand += Quantity1(i) * Price(i)
            TotalCostOnOrder += Quantity2(i) * Price(i)
        Next

        'Me.lblInventory = TotalQuantityOnHand  ' is this how I get the total books on hand to show on the form? I get an
        ' error that says Value of type Integer cannot be converted to Ststem.Windows.Forms.Label




    End Sub
End Class
0
CodeCruiserCommented:
>Me.lblInventory = TotalQuantityOnHand

It needs to be
Me.lblInventory.Text = TotalQuantityOnHand
0
joylene6Author Commented:
the form I am trying to put the data on
Doc2.doc
0
joylene6Author Commented:
That worked!!!

Now- I am still getting that pesky error.System.ArgumentNullException. I am trying to fill this list box with the data from the warehouse.txt so I can actually get data into the report.


 ' The Combo ListBox object is filled with IDs
            For intFill = 0 To 16
                Me.lstBooks.Items.Add(_strItemId(intFill))
            Next
        Else
            MsgBox(strFileError, , "Error")
            Me.Close()
        End If

    End Sub
0
joylene6Author Commented:
I think I have to add a second form to get this data. I will work on this and repost if I need help
0
CodeCruiserCommented:
You need to use correct arrays

_strItemId(intFill) is it the right array?
0
joylene6Author Commented:
oh - yes it is
0
joylene6Author Commented:
I changed it to this and still getting that error.  All I want to do is take the Book name and place it in the combo list box. The book name comes from the warehouse.txt file

 The Combo ListBox object is filled with IDs


            For intFill = 0 To (_strItemId.Length - 1)
                Me.lstBooks.Items.Add(_strItemId)
            Next
        Else
            MsgBox(strFileError, , "Error")
            Me.Close()
        End If

    End Sub
0
CodeCruiserCommented:
>Me.lstBooks.Items.Add(_strItemId)

Change to

Me.lstBooks.Items.Add(_strItemId(intFill))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joylene6Author Commented:
Thanks for being patient with me. I used your help and I did get this thing figured out. Thanks.
0
CodeCruiserCommented:
Glad that your problem is solved :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.