10 Replies - 371 Views - Last Post: 09 February 2012 - 06:32 PM Rate Topic: -----

Topic Sponsor:

#1 jurdendurden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 01-November 07

Object reference not set to an instance of an object.

Posted 01 February 2012 - 07:20 PM

I am creating a graphical tile game in VB.net which uses a map class I have created to give me the ability to have multiple maps in memory at once. The issue, however, is that I seem to be instantiating it incorrectly. When I create the map and try to fill it with some generic tiles, it gives me the above error. Here is the code behind it:

 Public Sub New(ByVal width As Integer, ByVal height As Integer, ByVal terrain As Integer)
        Dim x As Integer
        Dim y As Integer

        Me.Width = width
        Me.Height = height

        ReDim Me.Tiles(width, height)

        For x = 0 To width
            For y = 0 To height
                If Percentage() > 20 Then
                    Me.Tiles(x, y).Sector = Sector_Types.FIELD
                Else
                    Me.Tiles(x, y).Sector = Sector_Types.WALL
                End If

                Me.Tiles(x, y).Walkable = Walkable(Me.Tiles(x, y).Sector)
                Me.Tiles(x, y).Image = TileImage(Me.Tiles(x, y).Sector)
            Next
        Next

    End Sub



And it is called here:

Dim mapTest As Map = New Map(10, 10, 1)


If this is not clear or not enough code is given please let me know and I can elaborate further.

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 125
  • View blog
  • Posts: 872
  • Joined: 16-March 11

Re: Object reference not set to an instance of an object.

Posted 01 February 2012 - 07:52 PM

What line is the error happening at? I can't see why this bit of code would throw that error, could you show us a bit more?

Edit: Learn something new every day. :)

This post has been edited by trevster344: 02 February 2012 - 09:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 756
  • View blog
  • Posts: 2,821
  • Joined: 02-July 08

Re: Object reference not set to an instance of an object.

Posted 01 February 2012 - 11:08 PM

Your trying to set properties and ReDim an array from the constructor, instead create the object then run a Public sub that handles that code - this way all variables/properties are done being created. Setting the width and height shouldn't be a problem from the constructor, but could be defaults too.
Was This Post Helpful? 0
  • +
  • -

#4 jurdendurden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 01-November 07

Re: Object reference not set to an instance of an object.

Posted 02 February 2012 - 10:07 AM

View Posttrevster344, on 01 February 2012 - 07:52 PM, said:

What line is the error happening at? I can't see why this bit of code would throw that error, could you show us a bit more?


It's happening the first time it tries to set a tile's sector...
Me.Tiles(x, y).Sector = Sector_Types.FIELD


I don't have time to try Hawk's suggestion but I will this afternoon and I will reply with the results. In the mean time, thanks to those who have replied already.
Was This Post Helpful? 0
  • +
  • -

#5 jurdendurden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 01-November 07

Re: Object reference not set to an instance of an object.

Posted 02 February 2012 - 08:33 PM

View Post_HAWK_, on 01 February 2012 - 11:08 PM, said:

Your trying to set properties and ReDim an array from the constructor, instead create the object then run a Public sub that handles that code - this way all variables/properties are done being created. Setting the width and height shouldn't be a problem from the constructor, but could be defaults too.



Ok so I changed around the constructor to this:

Public Sub New(ByVal width As Integer, ByVal height As Integer, ByVal terrain As Integer)
        
        Me.Width = width
        Me.Height = height

End Sub



And created a separate public sub outside of the class itself that looks like this:


  Public Sub FillMap(ByVal TheMap As Map)
        Dim x As Integer
        Dim y As Integer


        ReDim TheMap.Tiles(TheMap.Width, TheMap.Height)

        With TheMap

            For x = 0 To .Width
                For y = 0 To .Height
                    If Percentage() > 20 Then
                        .Tiles(x, y).Sector = Sector_Types.FIELD
                    Else
                        .Tiles(x, y).Sector = Sector_Types.WALL
                    End If

                    .Tiles(x, y).Walkable = .Walkable(.Tiles(x, y).Sector)
                    .Tiles(x, y).Image = .TileImage(.Tiles(x, y).Sector)
                Next
            Next

      End With
    End Sub




And I'm still getting the same error the second it tries to set .Tiles(0,0).Sector to anything....

Also just to note, I tried it without the ReDim statement as well.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 756
  • View blog
  • Posts: 2,821
  • Joined: 02-July 08

Re: Object reference not set to an instance of an object.

Posted 02 February 2012 - 08:38 PM

The FillMap sub belongs in the same class as the constructor you were building. The thought is create the object then call this sub.

Dim _map As New Map(wd, ht)
'cTor is done so handle all other intializations here
_map.FillMap() 

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • Coder-ian
  • member icon

Reputation: 1401
  • View blog
  • Posts: 7,358
  • Joined: 29-May 08

Re: Object reference not set to an instance of an object.

Posted 02 February 2012 - 08:59 PM

When you create a collection (array include), the collection will contain the default for that kind of type. For red based (ie Class) that is going to (a no ref / null) or Nothing.

So how can it access the propertied and methods of an instance of that type (via the indexers), when that location has no reference to an instance?
 .Tiles(x, y).Sector = Sector_Types.FIELD



You need to create an instance for each location in the map.
Was This Post Helpful? 0
  • +
  • -

#8 jurdendurden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 01-November 07

Re: Object reference not set to an instance of an object.

Posted 03 February 2012 - 03:09 PM

View Post_HAWK_, on 02 February 2012 - 08:38 PM, said:

The FillMap sub belongs in the same class as the constructor you were building. The thought is create the object then call this sub.

Dim _map As New Map(wd, ht)
'cTor is done so handle all other intializations here
_map.FillMap() 



Did this just as you suggested and getting the same exact error.... What am I doing wrong??

 Public Sub New(ByVal width As Integer, ByVal height As Integer, ByVal terrain As Integer)
        
        Me.Width = width
        Me.Height = height

    End Sub

    Public Sub FillMap()
        Dim x As Integer
        Dim y As Integer


        'ReDim Me.Tiles(Me.Width, Me.Height)

        With Me

            For x = 0 To .Width
                For y = 0 To .Height
                    If Percentage() > 20 Then
                        .Tiles(x, y).Sector = Sector_Types.FIELD
                    Else
                        .Tiles(x, y).Sector = Sector_Types.WALL
                    End If

                    .Tiles(x, y).Walkable = .Walkable(.Tiles(x, y).Sector)
                    .Tiles(x, y).Image = .TileImage(.Tiles(x, y).Sector)
                Next
            Next

        End With
    End Sub


And this calls it:

Dim mapTest As Map = New Map(10, 10, 1)

        mapTest.FillMap()

Was This Post Helpful? 0
  • +
  • -

#9 jurdendurden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 01-November 07

Re: Object reference not set to an instance of an object.

Posted 03 February 2012 - 03:16 PM

Tile and Map classes in their entirety....

Public Class Tile
    Public tWalkable As Boolean
    Public tSector As Integer
    Public tImage As String

    Public Property Walkable() As Boolean
        Get
            Return tWalkable
        End Get
        Set(ByVal value As Boolean)
            tWalkable = value
        End Set
    End Property

    Public Property Sector() As Integer
        Get
            Return tSector
        End Get
        Set(ByVal value As Integer)
            tSector = value
        End Set
    End Property

    Public Property Image() As String
        Get
            Return tImage
        End Get
        Set(ByVal value As String)
            tImage = value
        End Set
    End Property

End Class


Public Class Map

    Private mWidth As Integer
    Public Property Width() As Integer
        Get
            Return mWidth
        End Get
        Set(ByVal value As Integer)
            mWidth = value
        End Set
    End Property

    Private mHeight As Integer
    Public Property Height() As Integer
        Get
            Return mHeight
        End Get
        Set(ByVal value As Integer)
            mHeight = value
        End Set
    End Property

    Private mDepth As Integer
    Public Property Depth() As Integer
        Get
            Return mDepth
        End Get
        Set(ByVal value As Integer)
            mDepth = value
        End Set

    End Property

    Public Tiles(Width, Height) As Tile

    Public Sub New(ByVal width As Integer, ByVal height As Integer, ByVal terrain As Integer)
        
        Me.Width = width
        Me.Height = height

    End Sub

    Public Sub FillMap()
        Dim x As Integer
        Dim y As Integer


        'ReDim Me.Tiles(Me.Width, Me.Height)

        With Me

            For x = 0 To .Width
                For y = 0 To .Height
                    If Percentage() > 20 Then
                        .Tiles(x, y).Sector = Sector_Types.FIELD
                    Else
                        .Tiles(x, y).Sector = Sector_Types.WALL
                    End If

                    .Tiles(x, y).Walkable = .Walkable(.Tiles(x, y).Sector)
                    .Tiles(x, y).Image = .TileImage(.Tiles(x, y).Sector)
                Next
            Next

        End With
    End Sub

    Public Sub DrawMap(ByRef gfx As Graphics)
        Dim x As Integer = 0
        Dim y As Integer = 0

        Dim image As Image

        For x = 0 To Me.Width
            For y = 0 To Me.Height
                image = New Bitmap(Me.Tiles(x, y).Image)
                gfx.DrawImage(image, x * 32, y * 32)
            Next
        Next
    End Sub

    Public Function GetSectorByTerrain(ByVal terrain As Integer)

        Select Case Percentage()
            Case Is < 80
                Return Sector_Types.FIELD
            Case Else
                Return Sector_Types.MARSHLAND

        End Select



    End Function

    Public Function Walkable(ByVal sector As Integer)

        Select Case sector

            Case Sector_Types.FIELD
            Case Sector_Types.MARSHLAND
                Return True

            Case Sector_Types.WALL
                Return False

        End Select

        Return True

    End Function

    Public Function TileImage(ByVal sector As Integer)

        Select Case sector

            Case Sector_Types.FIELD
                Return "c:/tiles/grass.png"

            Case Sector_Types.MARSHLAND
                Return "c:/tiles/marsh.png"

            Case Sector_Types.WALL
                Return "c:/tiles/wall.png"

        End Select

        Return "c:/tiles/grass.png"

    End Function
End Class

This post has been edited by AdamSpeight2008: 03 February 2012 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • Coder-ian
  • member icon

Reputation: 1401
  • View blog
  • Posts: 7,358
  • Joined: 29-May 08

Re: Object reference not set to an instance of an object.

Posted 03 February 2012 - 03:29 PM

Read my post again. Understand what it is telling you.
Was This Post Helpful? 0
  • +
  • -

#11 jurdendurden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 01-November 07

Re: Object reference not set to an instance of an object.

Posted 09 February 2012 - 06:32 PM

View PostAdamSpeight2008, on 03 February 2012 - 03:29 PM, said:

Read my post again. Understand what it is telling you.



By turning my tile class into a structure everything began working just fine, because each tile was not being instantiated when I would instantiate a new Map object. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1