0

For some odd reason I am getting an error when I am using my loop to display the elements of my array. I can't seem to understand what it is I am doing or not doing right. This is the code so far. This is not for a class, I am learning myself.

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

Private strExams(49, 2) As String
Dim count As Integer = 0

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim strStudent As String = txtStudent.Text
    Dim strTest As String = txtTest.Text
    Dim strScore As String = txtScore.Text

    If count <= 49 Then
        strExams(count, 0) = strStudent
        strExams(count, 1) = strTest
        strExams(count, 2) = strScore
        count += 1
    End If

    txtStudent.Text = String.Empty
    txtTest.Text = String.Empty
    txtScore.Text = String.Empty

    txtStudent.Focus()

End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

    Dim intHighRow As Integer = strExams.GetUpperBound(0)
    Dim intHighCol As Integer = strExams.GetUpperBound(1)
    Dim intR As Integer
    Dim intC As Integer
    Do While intC <= intHighCol
        intR = 0
        Do While intR <= intHighRow
            lstMessage.Items.Add(strExams(intR, intC))
            intR += 1
        Loop
        intC += 1
    Loop

End Sub

This is the error I am getting when I click my display button.

An unhandled exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll

Additional information: Value cannot be null.

3
  • What are you trying to achieve here when clicking the btnDisplay button? Commented Jan 20, 2014 at 1:23
  • I am trying to put all the contents of the array into the listbox. Commented Jan 20, 2014 at 1:27
  • I seemed to have mixed up how I wanted it to be listed. Its supposed to be listed by row not by column. Commented Jan 20, 2014 at 1:32

1 Answer 1

1

Try this. This makes much more sense to me. The reason you're getting a null error is that you haven't filled everything up in your array and your listbox can't list null items. So a workaround would be to enumerate only items that already have values, thus, just loop until the last value of count.

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim intR As Integer

    lstMessage.Items.Clear()
    Do While intR < count
        lstMessage.Items.Add(strExams(intR, 0) & " - " & strExams(intR, 1) & " - " & strExams(intR, 2))
        intR += 1
    Loop
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking it was something like that but was unsure of how to add all the columns in to get it to add to the listbox. Again thank you.
You're welcome. The default listbox control can only have 1 column, so I added a '-' character to separate values. If you want to display in multicolumn format, you'll have to use the ListView, or you can use 3rd party listbox controls which have the feature to list multicolumn format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.