1 Replies - 225 Views - Last Post: 13 July 2014 - 09:21 PM Rate Topic: -----

#1 kowilliams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-July 10

Null reference exception error

Posted 13 July 2014 - 08:32 PM

While debugging a game I was thrown with a System.NullReferenceException Error: Variable 'fObj' is used before it has been assigned a value. A null reference exception could result at runtime. The code in question is highlighted in bold.
Public Sub drawPlayer()
    drawWorld()
   [b] If ResourceLib.ImageExists("Player_" + wGrid(PlayerPos.X, PlayerPos.Y).TileName) = False Then[/b]
        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


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 post has been edited by modi123_1: 13 July 2014 - 09:20 PM
Reason for edit:: please use the code tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Null reference exception error

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,808
  • Joined: 12-June 08

Re: Null reference exception error

Posted 13 July 2014 - 09:21 PM

03	   [b] If ResourceLib.ImageExists("Player_" + wGrid(PlayerPos.X, PlayerPos.Y).TileName) = False Then[/b]

When you put a break point in that line, and mouse over the variables, which one is null/nothing?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1