1

I'm in the process of creating a tile based game. This game requires a method to load a text file and write the numbers between the delimiter "-", to a multidimensional array. However, an error message "object not set to an instance occurs.

'Load map
Public Sub LoadMap(ByVal URI As String)

    Using reader As New System.IO.StreamReader(URI)

        For x As Integer = 0 To 13

            Dim line = reader.ReadLine()

            Dim tokens() As String = line.Split("-")

            'adds values to multidimensional array
            For y As Integer = 0 To 16
                Me.map(x, y) = Integer.Parse(tokens(y))
            Next y
        Next x
    End Using
End Sub

Example map - numbers represent image id's

 2-2-2-0-0-0-0-0-0-0-0-3-3-5-5-5-5
2-2-2-0-0-0-0-0-0-0-0-3-3-5-5-5-5
2-2-2-0-0-0-0-0-0-0-0-3-3-2-2-2-5
2-2-2-0-0-0-0-0-0-0-0-3-3-2-2-2-5
2-2-2-0-0-0-0-0-0-0-0-3-3-2-2-2-5
0-0-0-0-0-0-0-0-0-0-0-3-3-2-2-2-5
3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2

I can't seem to establish the problem. Thanks in advance...

4 Answers 4

2
  • Always use Option Strict On! (Your code shouldn’t even compile, you need to parse the input integers, or your map stores strings, which is equally as bad since you logically store numbers.)
  • Name your variables properly.
  • Omit vacuous comments. Only comment things that aren’t obvious from the code.
  • Don’t hard-code magic numbers (what’s 11? 8?)
  • Don’t declare variables before initialising. Declare on first use.
  • The outer loop in your code makes no sense.
  • Don’t close streams manually, always use a Using block to ensure the program still works in the face of exceptions.
  • Initialise map.

Which leaves us with:

Public Sub LoadMap(ByVal URI As String)
    Const MapHeight As Integer = 12
    Const MapWidth As Integer = 9

    Me.map = New Integer(MapHeight, MapWidth) { }

    Using reader As New System.IO.StreamReader(URI)
        For x As Integer = 0 To MapHeight - 1
            Dim line = reader.ReadLine()
            Dim tokens() As String = line.Split("-")

            For y As Integer = 0 To MapWidth - 1
                Me.map(x, y) = Integer.Parse(tokens(y))
            Next y
        Next x
    End Using
End Sub

Bonus: Check for errors: what if the map doesn’t have the predefined width/height? Why hard-code this at all?

Sign up to request clarification or add additional context in comments.

Comments

2

osRead.ReadLine() returns Nothing if the end of the input stream is reached. You call Peek to see if you're at the end of the input, but then, you proceed to read 12 lines without checking if you're at the end of the input in-between. If you have less than 12 more lines, you'll get the error you mentionned on temp.Split("-"), because temp will have a value of Nothing, so you can't call methods on it.

Also, just noticed something... your Map is 11x8, but you're reading 12 lines, and going through 9 values, you probably want to do:

For x As Integer = 0 To 10

or

For x As Integer = 1 To 11

Same thing for your other loop.

Comments

1

If temp is null (Nothing) in VB.Net, then you can't call methods on it.

Do a check for Nothing before attempting to do anything with the value.


So Just to be sure you have updated your code to look something like this:

If temp IsNot Nothing
    'tempLine stores the split read line
    Dim tempLine() As String

    'splits readline into - ERROR Not set to an instance
    tempLine = temp.Split("-")

    'adds values to multidimensional array
    For y As Integer = 0 To 8
    Me.map(x, y) = tempLine(y)
    Next y
End If

And you are STILL getting the null reference exeption?

3 Comments

Same error message... "Object reference not set to an instance of an object."
Your comments seem to indicate it is originating from this line tempLine = temp.Split("-") Is this what the stack trace indicates, or does it show you a different line?
Solved! Thanks, a combination of your's and Meta-Knight advice has helped. Thanks ever so much.
0

also make sure that your StreamReader is properly initialized... if the initialization fails (perhaps because of a bad URI) then attempting to call osRead.peek() will throw the "object not set to an instance" error.

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.