0

Hello I recieve a "nullreferenceexception was unhandled" error when I try to run this code:

        For i As Integer = 1 To aantaltags
            csvopc(i) = csvtagssplit(17 * i)
            csvsql(i) = csvtagssplit(17 * i + 15)
        Next

Background: I read a csv file that I clean up and split into csvtagssplit()

csvopc and csvsql are both declared as string() at the top of the program.

anything dumb I did and I'm not noticing?

replicate it if you want to: code: http://pastebin.com/JDPa6FSB csv: http://pastebin.com/2e66i9EB

user2376356
  • 11
  • 1
  • 5
  • Which line throws the exception? – Steven Doggart May 15 '13 at 14:07
  • 1
    I don't see anywhere the declaration and dimensioning of the cvsopc and cvssql. I think that you should show how you have declared and initialized these two – Steve May 15 '13 at 14:07
  • http://pastebin.com/V18GX5X9 csvopc(i) = csvtagssplit(17 * i) throw the exception – user2376356 May 15 '13 at 14:10
  • Are you sure the `i` starts from `1` to `aantaltags` instead of `0` to `aantaltags - 1`? – ajakblackgoat May 15 '13 at 14:11
  • You did fix part of my program it does have to be "aantaltags -1" But it's the reason for the error. http://pastebin.com/2e66i9EB this is the csv if you're interested in testing it yourself. http://pastebin.com/JDPa6FSB make a form with a button. – user2376356 May 15 '13 at 14:19

2 Answers2

0

You should step through your code with a debugger in order to inspect the data as the code runs. You could put a breakpoint at a place where everything should be initialized but before the exception happens and then see what the values are.

Which iteration of the loop fails, and what line specifically fails? Put a breakpoint on that line and see what all the values are immediately before the line executes. If this debugging doesn't reveal the error to you, update your post with the data you find during debugging and maybe we can get somewhere.

0

Your problem is in the intial part of your code where you declare the two variables cvsopc and cvssql,

As from your comment you write

Dim csvopc As String() 
Dim csvsql As String()

But this only declares the two variables without any dimension.
So when you try to reach csvopc(i) you are effectively referencing a index that doesn't exist

Why use arrays when you don't know the exact size of your elements?. You can easily switch to a List(Of String) where you can dinamically add elements

Dim csvopc As List(Of String) = new List(Of String)
Dim csvsql As List(Of String) = new List(Of String)

and then in your loop

    For i As Integer = 0 To aantaltags - 1
        csvopc.Add(csvtagssplit(17 * i))
        csvsql.Add(csvtagssplit(17 * i + 15))
    Next

A List(Of String) could also be referenced by Index as in

   Dim aValue = csvopc(0)
Steve
  • 213,761
  • 22
  • 232
  • 286
  • What is the use of an array then? Isn't an array really just a list of data with the same datatype? – user2376356 May 15 '13 at 14:26
  • The use of arrays is still useful when you need speed and when you know the exact dimensions of your array. In this particular case the more modern List is an obvious winner – Steve May 15 '13 at 14:29
  • When you say speed, Will I get bad performance once the csv gets too large because I'm using a list instead of array? – user2376356 May 15 '13 at 14:30
  • You need to measure, but I don't think that the difference in performace is noticeable. [There is this interesting question](http://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists) about arrays vs list performance – Steve May 15 '13 at 14:32
  • Thanks, I guess I'm replacing all my arrays. – user2376356 May 15 '13 at 14:33
  • This brought up a new error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" except the index is not out of range, when i=1 it puts it into csvopc(1) which is valid and it copies the value from csvtassplit(17) which is also valid since it has a size 596 length. Are you forced to use the csvopc.add command? is csvopc(i)= no longer valid? – user2376356 May 15 '13 at 14:47
  • Difficult to say. Try with the debugger setting a breakpoint inside the loop and checking the value of `i` In particular I suspect of the last `csvtagssplit(17 * i + 15)` that could exit the range when i is in the last line – Steve May 15 '13 at 15:17
  • It's a list issue, when I put an empty string in before I assign it it's real value it works fine. I've tried changing the capacity of the list up front but it doesn't fix anything. Adding "" before I put in the real string works but if I use the loop more then once it'll just get twice as big because it keeps adding "". Do you know how I could fix this? – user2376356 May 15 '13 at 15:26
  • [List(Of T).Clear](http://msdn.microsoft.com/en-us/library/dwb5h52a.aspx) and remember that the index for the list (and arrays) starts at zero not at one – Steve May 15 '13 at 15:57