1

I have this code here:

Dim MasterIndex As String()()

Private Function Lookup(ByVal Search_path As String) As Integer
    Dim i As Integer = 0
    Do Until MasterIndex(i)(0) Is Nothing
        If Search_path = MasterIndex(i)(0) Then
            Return MasterIndex(i)(1)
        End If
    Loop
    Return -1
End Function

Which gives me the error Object reference not set to an instance of an object occuring on the Do Until line. Why is this? How can I fix this?

jayjyli
  • 771
  • 3
  • 11
  • 23
  • I would put a break point on Dim i As Integer = 0 and see whats in masterIndex to try to narrow down the issue. you can also wrap the block in a try catch to see if that clarifies whats going on. This isn't a solution but it may help you identify the problem. – Chris Santiago Oct 14 '11 at 20:42

2 Answers2

4

The MasterIndex variable is never assigned that is why you have the exception

You should instantiate MasterIndex first by calling the New() constructor:

 Dim MasterIndex As new String()()

and fill it with data before calling the Lookup function.

Something like:

 Private MasterIndex As String()() = New String()() {New String() {"A1", "A2"}, New String() {"B1", "B2"}}
mhttk
  • 1,688
  • 1
  • 16
  • 29
1

Either MasterIndex is not initialized or MasterIndex(0) is not initialized.

Can you show the code that initializes that variable, assuming you do that somewhere else in the program?

What happens if you put a breakpoint on that line and examine MasterIndex?

Eric J.
  • 147,927
  • 63
  • 340
  • 553