Click here to Skip to main content
15,507,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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.
Posted
Updated 9-Jul-14 17:42pm
v2

1 solution

You need to debug and find which is null at run time. read Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] if you need more information about debugging.
after you found the object which is null and the inputs which is reason for the generate null value, you can easily find a solution.
 
Share this answer
 
Comments
Keith O. Williams 10-Jul-14 10:10am    
I found the reference that is null at runtime

drawPlayer()
If Not fObj Is Nothing Then
fObj.Focus()
End If

Any suggestions on how to clear up this error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900