hello, i also get the "System.NullReferenceException: Object reference not set to an instance of an object" in my code
i declare two classes:
Code:
Public Class Boardsquare
Public Occupied As Boolean
Public Selected As Boolean
Public Content As New Piece
Sub New()
Occupied = Nothing
Selected = Nothing
Content = Nothing
End Sub
Public Sub Clear()
Occupied = False
Selected = False
Content.Clear()
End Sub
End Class
Public Class Piece
Public PcTeam As String
Public PcName As String
Public PcCode As String
Public PcKills As Integer
Public PcXCoor As Integer
Public PcYCoor As Integer
Sub New()
PcCode = " "
PcName = " "
PcTeam = " "
PcKills = 0
End Sub
Public Sub Clear()
PcTeam = " "
PcName = " "
PcCode = " "
PcKills = 0
PcXCoor = -2
PcYCoor = -2
End Sub
End Class
and declare a TWO DIMENSIONAL variable of type "Boardsquare"
Code:
Public board(,) As Boardsquare = New Boardsquare(8, 8) {}
but when i call the function to assign values to board:
Code:
While ctx < 8
While cty < 8
board(ctx, cty).Content.PcCode = " "
board(ctx, cty).Content.PcName = " "
board(ctx, cty).Content.PcTeam = " "
board(ctx, cty).Content.PcKills = 0
cty = cty + 1
End While
ctx = ctx + 1
End While
i get this error: "System.NullReferenceException: Object reference not set to an instance of an object."
i use two dimensional arrays since im building an 8x8 game board.
Creating array will not instantiate each object held by each index of the array. After creation of array , you have to instatiate each object separatly in each index like:
Public board(,) As Boardsquare = New Boardsquare(8, 8) {}
board(0,0) = new Boardsquare()
board(0,1) = new Boardsquare()
.... so on. Of course you can use a loop.
Please Mark As Answer if it helped.
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
Marked as answer by cloudburst on Dec 29, 2011 06:37 AM
cloudburst
Member
4 Points
2 Posts
Object reference not set to an instance of an object error
Dec 26, 2011 09:17 AM|LINK
hello, i also get the "System.NullReferenceException: Object reference not set to an instance of an object" in my code
i declare two classes:
Code:
Public Class Boardsquare Public Occupied As Boolean Public Selected As Boolean Public Content As New Piece Sub New() Occupied = Nothing Selected = Nothing Content = Nothing End Sub Public Sub Clear() Occupied = False Selected = False Content.Clear() End Sub End Class Public Class Piece Public PcTeam As String Public PcName As String Public PcCode As String Public PcKills As Integer Public PcXCoor As Integer Public PcYCoor As Integer Sub New() PcCode = " " PcName = " " PcTeam = " " PcKills = 0 End Sub Public Sub Clear() PcTeam = " " PcName = " " PcCode = " " PcKills = 0 PcXCoor = -2 PcYCoor = -2 End Sub End Classand declare a TWO DIMENSIONAL variable of type "Boardsquare"
Code:
Public board(,) As Boardsquare = New Boardsquare(8, 8) {}but when i call the function to assign values to board:
Code:
While ctx < 8 While cty < 8 board(ctx, cty).Content.PcCode = " " board(ctx, cty).Content.PcName = " " board(ctx, cty).Content.PcTeam = " " board(ctx, cty).Content.PcKills = 0 cty = cty + 1 End While ctx = ctx + 1 End Whilei get this error: "System.NullReferenceException: Object reference not set to an instance of an object."

i use two dimensional arrays since im building an 8x8 game board.
please help
adeelehsan
All-Star
18593 Points
2789 Posts
Re: Object reference not set to an instance of an object error
Dec 26, 2011 09:25 AM|LINK
Creating array will not instantiate each object held by each index of the array. After creation of array , you have to instatiate each object separatly in each index like:
Public board(,) As Boardsquare = New Boardsquare(8, 8) {}
board(0,0) = new Boardsquare()
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
sudhalaksh
Participant
815 Points
176 Posts
Re: Object reference not set to an instance of an object error
Dec 26, 2011 09:45 AM|LINK
Hi Friend,
Please add the following code in your while loop(While cty < 8).
Now it will work fine..
Thanks.
cloudburst
Member
4 Points
2 Posts
Re: Object reference not set to an instance of an object error
Dec 29, 2011 06:36 AM|LINK
thanks adeelehsan and sudhalaksh. great help