-1

I have my code that is reading values from a database and then putting them into an array. I want to copy the values of this array to another array, however when I try, it keeps on throwing an exception,

Object reference not set to an instance of an object.

I don't seem to see what is wrong. How can I fix this problem?

My code is here:

    ' Read values from a database and assign them to an array
    Dim counter As Integer = 0
    Dim reader As MySqlDataReader
    reader = cmd.ExecuteReader()
    Dim dataarray As string()

    While reader.Read()
        Dim datatoAdd As string = reader.GetValue(1) & ", " & _
                                  reader.GetValue(2) & ", " & _
                                  reader.GetValue(3) & ", " & _
                                  reader.GetValue(6) & ", " & _
                                  reader.GetValue(7) & ", " & _
                                  reader.GetValue(8) & ", " & _
                                  reader.GetValue(9) & ", " & _
                                  reader.GetValue(10) & ", " & _
                                  reader.GetValue(11)

        dataarray(counter) = datatoAdd
    End While

    reader.Close()
    connection.Close()
    MessageBox.Show('Data added successfully)

Catch ex As Exception
    MessageBox.Show(ex.Message)
1
  • 1
    Is it possible that you are missing a counter += 1 inside your While loop?
    – Heinzi
    Aug 2, 2011 at 8:52

2 Answers 2

4

Your statement

Dim dataarray As string()

just declared dataarray -- you defined that dataarray can point to an array of string. Currently, it does not point anywhere. You have to create a new array and assign it to dataarray.

The easiest way to do so is to specify the size of the array in the declaration:

Dim dataarray(UPPER_BOUND) As String

This will create an array with indexes 0 to UPPER_BOUND (i.e., with UPPER_BOUND+1 elements). (If you don't know the upper bound yet, don't use an array, but use a List(Of String) instead.)

More details:


EDIT: You state in the comments that you don't know the size of the array. In that case you shouldn't be using an array at all. Arrays are by definition fixed-size data structures. (Yes, there's ReDim Preserve, but it's still not a good idea.)

Instead, use a resizable data structure such as List(Of String). If you really need an array, you can convert it into an array afterwards.

10
  • @Staypa: You replace Dim dataarray As String() with Dim dataarray(5) As String (if you want your array to have 6 elements -- otherwise, replace 5 with the upper bound you want).
    – Heinzi
    Aug 2, 2011 at 8:49
  • Its a database so I dont know the size of the array. I cant determine it size
    – Staypa
    Aug 2, 2011 at 8:56
  • @Staypa: Then you shouldn't be using an array. Use a resizable data structure such as List(Of String) instead. If you really need an array, you can convert it into an array afterwards.
    – Heinzi
    Aug 2, 2011 at 9:05
  • 1
    @Staypa: ArrayList is an old data structure. If you can, use a List(Of String) instead. It has the same features as an ArrayList, but it's also type safe.
    – Heinzi
    Aug 2, 2011 at 9:24
  • 2
    @Staypa: But you asked for array, not for arraylist. ;-) Use a List(Of Integer) (or whatever data type your values are), Max() should work there.
    – Heinzi
    Aug 2, 2011 at 9:51
0

VB arrays start at 0 and not 1 don't they? Could that be the problem?

See - http://patorjk.com/programming/tutorials/vbarrays.htm

3
  • 1
    It's not the problem in this case. Heinzi has it correct. (BTW I did not downvote)
    – MarkJ
    Aug 2, 2011 at 11:56
  • I did wonder, should have left this as a comment really as it wasn't a full answer. Hindsight is a wonderful thing. Still, it might be useful to someone who comes along :) Aug 2, 2011 at 12:51
  • Usually they start at zero. You can really botch it up though by treating it as if it starts at 1
    – Grantly
    Jul 2, 2015 at 12:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.