Arrays VB.NET |面板中的pictureboxes数组:对象引用未设置为对象的实例
在下面的代码中,我试图更改在数组“pictureboxes(9,9)”中创建的现有控件的标记。尝试执行此操作时,会出现错误“对象引用未设置为对象的实例”。这是在代码底部附近的checkdata子项中完成的,注释为“对象引用未设置为对象的实例。” 传递给sub的字符串是一个长的数字字符串,如果有帮助的话,PictureBoxe将被放置到layoutpanel中 我理解这个错误的意思,我知道pictureboxes(I,j)在被中断时等于零;我只是不知道怎么修理它 非常感谢您的帮助,希望我能尽快回答您的任何意见/答案 多谢各位Arrays VB.NET |面板中的pictureboxes数组:对象引用未设置为对象的实例,arrays,vb.net,picturebox,Arrays,Vb.net,Picturebox,在下面的代码中,我试图更改在数组“pictureboxes(9,9)”中创建的现有控件的标记。尝试执行此操作时,会出现错误“对象引用未设置为对象的实例”。这是在代码底部附近的checkdata子项中完成的,注释为“对象引用未设置为对象的实例。” 传递给sub的字符串是一个长的数字字符串,如果有帮助的话,PictureBoxe将被放置到layoutpanel中 我理解这个错误的意思,我知道pictureboxes(I,j)在被中断时等于零;我只是不知道怎么修理它 非常感谢您的帮助,希望我能尽快回答
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Net
Public Class Form1
'0 as default tile, 1 as clicked, 3 as set mine (host perspective)
Dim tiles() As Integer = {0}
Public pictureboxes(9, 9) As PictureBox
Dim flagged() As Integer
Dim clicked As Integer()
Dim columns As Integer = 10
Dim rows As Integer = 10
Dim placedMinesCount As Integer = 0
Dim formattedTag As String()
Public turn As Boolean = False
Dim stringToSend As String
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Byte = 0 To 9
For j As Byte = 0 To 9
pictureboxes(i, j) = New PictureBox
pictureboxes(i, j).Height = 60
pictureboxes(i, j).Width = 60
pictureboxes(i, j).ImageLocation = "0.png"
pictureboxes(i, j).Tag = "0|" & i & ", " & j
AddHandler pictureboxes(i, j).Click, AddressOf Tile_Click
Dim column As Integer = j
Dim row As Integer = i
Panel.Controls.Add(pictureboxes(i, j), column, row)
Next
Next
If Login.isHost = True Then
Me.Text = "Set your mines (10)"
turn = True
ElseIf Login.isHost = False Then
Me.Text = "Await your turn"
btnEndTurn.Visible = False
btnEndTurn.Enabled = False
End If
End Sub
Protected Sub Tile_Click(ByVal sender As Object, ByVal e As EventArgs)
formatSenderTag(sender.tag)
If Login.isHost = True Then
Dim clickAction As String = formattedTag(0)
Select Case clickAction
Case "0"
If placedMinesCount < 10 Then
placedMinesCount = placedMinesCount + 1
sender.imagelocation = "3.png"
sender.tag = "3"
Me.Text = "Set your mines (" & 10 - placedMinesCount & ")"
ElseIf placedMinesCount >= 10 Then
MsgBox("You have placed all of your 10 Mines")
End If
Case "2"
MsgBox("You cannot set a mine here")
Case "3"
placedMinesCount = placedMinesCount - 1
Me.Text = "Set your mines (" & 10 - placedMinesCount & ")"
sender.imagelocation = "0.png"
sender.tag = "0"
End Select
ElseIf Login.isHost = False Then
Dim clickAction As String = formattedTag(0)
Select Case clickAction
Case "0"
sender.tag = "1"
sender.imagelocation = "1.png"
Case "3"
MsgBox("Game Over")
Case "2"
MsgBox("Already Clicked")
End Select
End If
End Sub
Private Sub formatSenderTag(ByVal sender As String)
'split into array
'element 0 as TILE TYPE
'element 1 as TILE LOCATION
formattedTag = sender.Split(New String() {"|"}, StringSplitOptions.None)
End Sub
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End
End Sub
Private Sub btnEndTurn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEndTurn.Click
turn = False
gridtostring()
senddata()
End Sub
Private Sub gridtostring()
For i As Byte = 0 To 9
For j As Byte = 0 To 9
formatSenderTag(pictureboxes(i, j).Tag & "|")
stringToSend = stringToSend & formattedTag(0)
Next
Next
End Sub
Private Sub senddata()
'***SEND STUFF
'Assuming you have a textbox with the data you want to send
If (Not String.IsNullOrEmpty(stringToSend)) Then
Dim data() As Byte = Encoding.ASCII.GetBytes(stringToSend)
Login.sendingClient.Send(data, data.Length)
End If
End Sub
Public Sub checkdata(ByVal data As String)
If Not data = stringToSend Then
'Dim loopcount As Integer = 0
For i As Byte = 0 To 9
For j As Byte = 0 To 9
Dim loopcount As Integer = (i.ToString & j.ToString) + 1
'Dim pineapple As String = pictureboxes(i, j).Tag
'pictureboxes(i, j).Tag = GetChar(data, 3)
pictureboxes(i, j).Tag = GetChar(data, loopcount) & "|" & i & ", " & j '***Object reference not set to an instance of an object.***
formatSenderTag(pictureboxes(i, j).Tag)
pictureboxes(i, j).ImageLocation = formattedTag(0)
pictureboxes(i, j).ImageLocation = "0.png"
'Panel.Controls(pictureboxes(i, j).Tag) = GetChar(data, 3) '.Tag = GetChar(data, 3)
Next
Next
End If
End Sub
End Class
***堆栈跟踪
at Minesweeper.Form1.checkdata(String data) in c:\users\harry\documents\visual studio 2010\Projects\Minesweeper\Minesweeper\Form1.vb:line 139
at Minesweeper.Login.Receiver() in C:\Users\Harry\Documents\Visual Studio 2010\Projects\Minesweeper\Minesweeper\Login.vb:line 71
at Minesweeper.Login._Lambda$__1(Object a0) in C:\Users\Harry\Documents\Visual Studio 2010\Projects\Minesweeper\Minesweeper\Login.vb:line 61
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
注意:某些地方的间距错误此代码不会给出您提供的数据错误:
checkdata("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
Public Sub checkdata(ByVal data As String)
If Not data = stringToSend Then
'Dim loopcount As Integer = 0
For i As Byte = 0 To 9
For j As Byte = 0 To 9
Dim loopcount As Integer = (i.ToString & j.ToString) + 1
'Dim pineapple As String = pictureboxes(i, j).Tag
'pictureboxes(i, j).Tag = GetChar(data, 3)
pictureboxes(i, j).Tag = GetChar(data, loopcount) & "|" & i & ", " & j '***Object reference not set to an instance of an object.***
formatSenderTag(pictureboxes(i, j).Tag)
pictureboxes(i, j).ImageLocation = formattedTag(0)
pictureboxes(i, j).ImageLocation = "0.png"
'Panel.Controls(pictureboxes(i, j).Tag) = GetChar(data, 3) '.Tag = GetChar(data, 3)
Next
Next
End If
End Sub
查看堆栈跟踪和代码,您缺少登录类中Form1的实例化。我认为这可能是问题所在
尝试将其添加到登录类的开头:
Dim Form1 as New Form1
但是当它在pictureboxes(0,0)上运行时会抛出错误,我肯定有0,0作为一个存在的东西。如果我硬编码数字并从该行中删除变量,它也会做同样的事情。
data
和该行的loopcount
的值是多少?此外,您的代码示例不包括对子checkdata
的调用。你也可以发布这个吗?这个异常的堆栈跟踪在哪里?如果您分析它,那么它将清楚地给您一行抛出异常的代码。您必须仔细研究跟踪路径。提供跟踪,以便可以为amswer提供数据的值为:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Loopcount的值为:1(数据的值是正确的,因为它们都是应用程序中的默认值,因为我没有为测试更改任何内容)其想法是,它将从数据中选择第一个数字,将其指定为pictureboxes数组中第一个事物的标记,然后对第二个事物etcThanks进行某某标记。请参阅上面的代码更新。它不会给出对象引用错误。所以你需要看得更深一些。正如Mukesh所问,堆栈跟踪是什么。这没什么错。你得到的错误是字面上说的。您试图访问一个不存在的对象。你能粘贴你的实际代码吗?根据跟踪,看起来好像是登录接收者发送了呼叫。您在登录类中的何处实例化Form1?你需要添加:暗淡的Form1作为新的Form1哈哈,是的,这似乎已经完成了。你能把这个评论作为一个答案吗?这样我就可以把它标记为最终答案了?谢谢:好的,很高兴听到!我更新了答案:
Dim Form1 as New Form1