Question Code for array check buggy

vabatta

Member
Joined
Jan 15, 2012
Messages
9
Programming Experience
Beginner
Hi all,
I'm collecting in an array the splitted text of a textbox in order to search in a bimensional array for matching in the name, but I got the error "NullReferenceException" at the red line.
Now, I looked the code multiple times, but I didn't find the error... Who can help me?

VB.NET:
[COLOR=#0000cd]Dim[/COLOR] info(0 [COLOR=#0000cd]To[/COLOR] 0, 0 [COLOR=#0000cd]To[/COLOR] 5) [COLOR=#339900]'this is array is already filled by a sub[/COLOR]
[COLOR=#0000cd]Dim[/COLOR] temp(0 [COLOR=#0000cd]To[/COLOR] 0) [COLOR=#0000cd]As String[/COLOR] [COLOR=#339900]'array temp[/COLOR]
[COLOR=#0000cd]Dim[/COLOR] temp2() [COLOR=#0000cd]As String[/COLOR] = Split(txtSearch.Text.ToLower, " ") [COLOR=#339900]'the splitted text from textbox, like "test 12"
[/COLOR][COLOR=#0000cd]For[/COLOR] s = 0 [COLOR=#0000cd]To[/COLOR] UBound(temp2, 1) [COLOR=#339900]'count for each split[/COLOR]
      [COLOR=#0000cd]For[/COLOR] i = 0 [COLOR=#0000cd]To[/COLOR] UBound(info, 1) [COLOR=#339900]'count for each info name[/COLOR]
           [COLOR=#0000cd]If[/COLOR] [COLOR=#ff0000]info(i, 1).ToLower.Contains(temp2(s))[/COLOR] [COLOR=#0000cd]Then[/COLOR] [COLOR=#339900]'if splitted text contains info name[/COLOR]
                [COLOR=#0000cd]ReDim Preserve[/COLOR] temp(0 [COLOR=#0000cd]To[/COLOR] UBound(temp) + 1) [COLOR=#339900]'redim the array temp + 1[/COLOR]
                temp(UBound(temp)) = i [COLOR=#339900]'save the value[/COLOR]
            [COLOR=#0000cd]End If[/COLOR]
      [COLOR=#0000cd]Next[/COLOR]
[COLOR=#0000cd]Next[/COLOR]
 
Last edited:
Firstly, whilst I can't find a reference to it, I assume the (x To x) in your array declarations are usually used to define a non-zero starting element - if that's the case, then why are you using them with zero as the starting element? Just define them as arrayname(x)

Secondly, have you stuck a breakpoint on your loop and seen what values your arrays? If not, then that's what you need to do.
 
So, for the first one I didn't understand what you mean, I just assign the value to arrayname(maxentry + 1), but anyway, I stucked with a breakpoint, and I still get the error, though values are this:


If info(i, 1).ToLower.Contains(temp2(s)) Then 'info(i, 1) = "Test Pointer", temp2(s) = "point"
 
Yes, you will still get the error when debugging - the point of adding a breakpoint and stepping through the code execution is to see what values your code is creating and how it is dealing with them.

Did you step through each iteration of your loop, checking what the values were? Are the values you posted above the ones when your code failed?

This should be very easy for you to debug - there's nothing complex going on (other than your array definitions)

With regards to your not understanding my point about your arrays - you have :
VB.NET:
Dim temp(0 To 0) As String
which is the same as saying
VB.NET:
Dim temp(0) As String
. Does that make it clearer?
 
Yes, thanks.
Now, I'm looking for the error in the loop... if I didn't find it, I think I will change the method on how to find splitted text in the name.
 
Okay, I found the error... when I create the array, I insert a null value, and that's was the error.

Thanks for help!
 
Back
Top