Object reference not set to an instance of an object errorhttp://forums.asp.net/t/1753113.aspx/1?Object+reference+not+set+to+an+instance+of+an+object+errorThu, 29 Dec 2011 06:36:35 -050017531134749885http://forums.asp.net/p/1753113/4749885.aspx/1?Object+reference+not+set+to+an+instance+of+an+object+errorObject reference not set to an instance of an object error <p>hello, i also get the &quot;System.NullReferenceException: Object reference not set to an instance of an object&quot; in my code<br> <br> i declare two classes:<br> <br> Code:</p> <pre class="prettyprint">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 = &quot; &quot; PcName = &quot; &quot; PcTeam = &quot; &quot; PcKills = 0 End Sub Public Sub Clear() PcTeam = &quot; &quot; PcName = &quot; &quot; PcCode = &quot; &quot; PcKills = 0 PcXCoor = -2 PcYCoor = -2 End Sub End Class</pre> <p>and declare a TWO DIMENSIONAL variable of type "Boardsquare"</p> <p>Code:</p> <pre class="prettyprint"> Public board(,) As Boardsquare = New Boardsquare(8, 8) {}</pre> <p>but when i call the function to assign values to board:</p> <p>Code:</p> <pre class="prettyprint"> While ctx &lt; 8 While cty &lt; 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 </pre> <p>i get this error: &quot;System.NullReferenceException: Object reference not set to an instance of an object.&quot;<br> <br> i use two dimensional arrays since im building an 8x8 game board.<br> <br> please help&nbsp;<img class="inlineimg" title="Frown" border="0" alt="" src="http://images.devshed.com/faf/smilies/frown.gif"></p> 2011-12-26T09:17:38-05:004749892http://forums.asp.net/p/1753113/4749892.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+object+errorRe: Object reference not set to an instance of an object error <p>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:</p> <pre class="alt2">Public board(,) As Boardsquare = New Boardsquare(8, 8) {}<br><br>board(0,0) = new Boardsquare()</pre> <pre class="alt2">board(0,1) = new Boardsquare()<br><br>.... so on. Of course you can use a loop.</pre> 2011-12-26T09:25:05-05:004749919http://forums.asp.net/p/1753113/4749919.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+object+errorRe: Object reference not set to an instance of an object error <p>Hi Friend,</p> <p>&nbsp;</p> <p>Please add the following code in your while loop(While cty &lt; 8).</p> <pre class="prettyprint">board(ctx, cty) = New Boardsquare() board(ctx, cty).Content = New Piece()</pre> <p>Now it will work fine..</p> <p>Thanks.</p> 2011-12-26T09:45:46-05:004754538http://forums.asp.net/p/1753113/4754538.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+object+errorRe: Object reference not set to an instance of an object error <p>thanks&nbsp;adeelehsan and sudhalaksh. great help</p> 2011-12-29T06:36:35-05:00