Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB10.0 VB.NET
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.
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 10-May-12 10:13am
Edited 10-May-12 12:52pm
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
losmac at 10-May-12 15:37pm
   
5+
SAKryukov at 10-May-12 15:42pm
   
Well explained, a 5.
--SA
Monjurul Habib at 11-May-12 1:18am
   
5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Comments
SAKryukov at 10-May-12 15:42pm
   
Makes sense -- education required. Very much. A 5.
--SA
losmac at 10-May-12 15:44pm
   
Thank you ;)
Monjurul Habib at 11-May-12 1:18am
   
5!
losmac at 11-May-12 1:26am
   
Thank you ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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
  Permalink  
v2
Comments
losmac at 10-May-12 15:37pm
   
5+
SAKryukov at 10-May-12 15:42pm
   
Thank you,
--SA
Monjurul Habib at 11-May-12 1:17am
   
5!
SAKryukov at 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
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 10 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100