Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi folks,

I'm reading from rows of a datatable and I'm trying to store the info under a certain column for each row in an array of strings . But I get the"Object reference not set to.." error.
VB
For index As Integer = 0 To SubTables.Count - 1
     Dim RefDesgStr() As String = Nothing
  Dim ii As Integer = 0

     For Each row As DataRow In SubTables(index).Rows

               RefDesgStr(ii) = row.Item("RefDesg")   ' Got the error here
    ' some code checking RefDesgStr(ii) with all the prior strings in RefDesgStr()
          ii +=1
      Next row
   index+=1
 next


I'm confused cuz I didn't get error if RefDesgStr was a simple string type and get overwritten each loop, instead of being an array of strings.
Posted
Updated 10-May-12 11:52am
v7

It doesn't work because you declared a variable that will point to an array of strings. You didn't actually CREATE an array of strings.

You need to ReDim Preserve the array to a known size in order for this to work.

Better yet, drop the array and use a List(Of String) instead. Then you don't need to screw around with resizing an array at all.

This:
For index As Integer = 0 To SubTables.Count - 1
    Dim RefDesgStr() As String = Nothing
    Dim ii As Integer = 0
    
    For Each row As DataRow In SubTables(index).Rows
        ReDim Preserve RefDesgStr(ii)
        RefDesgStr(ii) = row.Item("RefDesg")   ' Got the error here
        ii+=1
    Next
Next


becomes this:
For index As Integer = 0 To SubTables.Count - 1
    Dim RefDesgStr As New List(Of String)
    Dim ii As Integer = 0
 
    For Each row As DataRow In SubTables(index).Rows
        RefDesgStr.Add(row.Item("RefDesg"))
    Next row
Next
 
Share this answer
 
Comments
Maciej Los 10-May-12 15:37pm    
5+
Sergey Alexandrovich Kryukov 10-May-12 15:42pm    
Well explained, a 5.
--SA
Monjurul Habib 11-May-12 1:18am    
5!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-May-12 15:42pm    
Makes sense -- education required. Very much. A 5.
--SA
Maciej Los 10-May-12 15:44pm    
Thank you ;)
Monjurul Habib 11-May-12 1:18am    
5!
Maciej Los 11-May-12 1:26am    
Thank you ;)
How could you possibly de-reference RefDesgStr if you explicitly initialized it as Nothing? This is all the problem. Instead, you could do something like:
VB
Dim RefDesgStr As String() = New String(SubTables.Count) {}


You consideration of "overridden in each loop" is totally wrong. You are working with the array of string type. When you assign a string value it its element, the element can be null, but not the whole object. If you are not getting it, I'm afraid you would rather learn language and programming nearly from the very beginning, to understand what is the type, reference or value type, reference, instance (object), array, array element and related very basic concepts.

Next time, always run your code under debugger and examine variables before the exception is thrown. This type of bug is one of the easiest to fix.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 10-May-12 15:37pm    
5+
Sergey Alexandrovich Kryukov 10-May-12 15:42pm    
Thank you,
--SA
Monjurul Habib 11-May-12 1:17am    
5!
Sergey Alexandrovich Kryukov 11-May-12 16:30pm    
Thank you, Monjurul.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900