While debugging a game I was thrown with a System.NullReferenceException Error: Object variable or With block variable not set for the following line of code in bold:
Public Sub drawPlayer()
drawWorld()
If ResourceLib.ImageExists("Player_" + wGrid(PlayerPos.X, PlayerPos.Y).TileName) = False Then
Dim newPImg As Bitmap = New Bitmap(playerImg)
Dim newImg As Bitmap = New Bitmap(wGrid(PlayerPos.X, PlayerPos.Y).Image, newPImg.Size)
For px As Integer = 0 To newImg.Width - 1
For py As Integer = 0 To newImg.Height - 1
Dim pixel As Color = newPImg.GetPixel(px, py)
If pixel <> Color.FromArgb(0, 255, 0) Then
newImg.SetPixel(px, py, pixel)
End If
Next
Next
ResourceLib.LoadImage("Player_" + wGrid(PlayerPos.X, PlayerPos.Y).TileName, newImg)
End If
If ExitPos.X > 0 And ExitPos.Y > 0 Then
If ResourceLib.ImageExists("Exit_" + wGrid(ExitPos.X, ExitPos.Y).TileName) = False Then
Dim newPImg As Bitmap = New Bitmap(exitImg)
Dim newImg As Bitmap = New Bitmap(wGrid(ExitPos.X, ExitPos.Y).Image, newPImg.Size)
For px As Integer = 0 To newImg.Width - 1
For py As Integer = 0 To newImg.Height - 1
Dim pixel As Color = newPImg.GetPixel(px, py)
If pixel <> Color.FromArgb(0, 255, 0) Then
newImg.SetPixel(px, py, pixel)
End If
Next
Next
ResourceLib.LoadImage("Exit_" + wGrid(ExitPos.X, ExitPos.Y).TileName, newImg)
End If
End If
For Each Obj As GObject In oList
If Obj.PositionMethod = GObject.PosMethod.World And Obj.Visible = True Then
If Obj.ImgTransColor <> Color.FromArgb(0, 0, 0, 0) Then
If ResourceLib.ImageExists(Obj.ImageFile + "_" + wGrid(Obj.Position.X, Obj.Position.Y).TileName) = False Then
Dim newOImg As Bitmap = New Bitmap(Obj.Image)
Dim newImg As Bitmap = New Bitmap(wGrid(Obj.Position.X, Obj.Position.Y).Image, newOImg.Size)
For px As Integer = 0 To newImg.Width - 1
For py As Integer = 0 To newImg.Height - 1
Dim pixel As Color = newOImg.GetPixel(px, py)
If pixel <> Color.FromArgb(0, 255, 0) Then
newImg.SetPixel(px, py, pixel)
End If
Next
Next
ResourceLib.LoadImage(Obj.ImageFile + "_" + wGrid(Obj.Position.X, Obj.Position.Y).TileName, newImg)
End If
dGrid(Obj.Position.X, Obj.Position.Y).Image = ResourceLib.GetImage(Obj.ImageFile + "_" + wGrid(Obj.Position.X, Obj.Position.Y).TileName)
Else
dGrid(Obj.Position.X, Obj.Position.Y).Image = Obj.Image
End If
End If
Next
Dim getObj As GObject = RayCastColl_GetObject(PlayerPos.X, PlayerPos.Y, PlayerDirection)
If Not getObj Is Nothing Then
lblLookingAt.Text = "Looking at: " + getObj.Name + " (" + getObj.Position.X.ToString + "," + getObj.Position.Y.ToString + ") [" + getObj.Key + "]"
If getObj.ObjectClass.StartsWith("npc_") Then lblLookingAt.Text += " (" + getObj.LocalVars.getVar("Health").ToString + " HP)"
Else
Dim lookAt As GTile = RayCastColl_GetTile(PlayerPos.X, PlayerPos.Y, PlayerDirection)
lblLookingAt.Text = "Looking at: " + lookAt.TileName
End If
dGrid(PlayerPos.X, PlayerPos.Y).Image = ResourceLib.GetImage("Player_" + wGrid(PlayerPos.X, PlayerPos.Y).TileName)
If ExitPos.X > 0 And ExitPos.Y > 0 Then dGrid(ExitPos.X, ExitPos.Y).Image = ResourceLib.GetImage("Exit_" + wGrid(ExitPos.X, ExitPos.Y).TileName)
Laser()
Update()
End Sub
How do I clear up this null reference exception error? I am using Visual Studio Professional 2010.