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?

1
  • 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. Commented Oct 14, 2011 at 20:42

2 Answers 2

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"}}
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.