求解,为什么我定义的类显示为nothing?这里的NullReferenceException究竟是源自哪里?

Lucaszha0 2017-03-25 11:27:38
Class Grid
Private width As Integer
Private height As Integer
Dim status(width, height) As Boolean
Public snake As Snake
Private food, initbody As Node
Private snakeDirection As Direction = Direction.LEFT
Public Sub Grid(width, height)
Me.width = width
Me.height = height
initSnake()
createFood()
End Sub
Private Function initSnake() As Snake
For i = width / 2 + 10 To width / 2 - 50 Step 10
initbody = New Node(i, height / 2 + 10)
snake.body.Add(initbody)
status(i, height / 2 + 10) = True
Next
Return snake
End Function
Public Function getSnake() As Snake
Return snake
End Function
Public Function getFood()
Return food
End Function
Public Function createFood() As Node
Dim x, y As Integer
Randomize()
x = (Fix(Rnd() * 21) + 1) * 10
y = (Fix(Rnd() * 21) + 1) * 10
food = New Node(x, y)
Return food
End Function
End Class


Class Gameview
Private grid As Grid
Private canvas As Panel
Private snake As Snake

Public Sub paintcomponent(graphics As Graphics)
drawGridBackground(graphics)
drawSnake(graphics, grid.getSnake())
drawFood(graphics, grid.getFood())
End Sub
Public Sub GameView(grid As Grid)
Me.grid = grid
End Sub
Public Sub draw(graphics As Graphics)
canvas.Refresh()
End Sub
Public Function getCanvas() As Panel
Return canvas
End Function
Public Sub drawSnake(graphics As Graphics, snake As Snake)
Dim rectanglehead As New Rectangle(snake.getHead.getX, snake.getHead.getY, 10, 10)
graphics.DrawPie(Pens.ForestGreen, rectanglehead, 0, 360)
For i = 2 To Len(snake.body)
Dim rectanglebody As New Rectangle(snake.body.Item(i).getX, snake.body.Item(i).getY, 10, 10)
graphics.DrawPie(Pens.LightGreen, rectanglebody, 0, 360)
Next
End Sub
Public Sub drawFood(graphics As Graphics, squareArea As Node)
Dim rectangle As New Rectangle(squareArea.getX, squareArea.getY, 10, 10)
graphics.DrawPie(Pens.Red, rectangle, 0, 360)
End Sub
Public Sub drawGridBackground(graphics As Graphics)
For i = 0 To 20
Dim point1 As New Point(10 + 10 * i, 10)
Dim point2 As New Point(10 + 10 * i, 210)
Dim point3 As New Point(10, 10 + 10 * i)
Dim point4 As New Point(210, 10 + 10 * i)
graphics.DrawLine(Pens.AliceBlue, point1, point2)
graphics.DrawLine(Pens.AliceBlue, point3, point4)
Next
End Sub
End Class

Class Node
Private x, y As Integer

Public Sub New(ByRef x, ByRef y)
MyClass.x = x
MyClass.y = y
End Sub
Public Function getX()
Return x
End Function
Public Function getY()
Return y
End Function
End Class

Class Snake
Public body As New Collection()
Private upnode, downnode, leftnode, rightnode As Node
Public Function eat(ByVal food As Node) As Node
If isNeighbor(body.Item(1), food) Then
body.Add(food)
End If
Return food
End Function
Private Function isNeighbor(ByVal a As Node, ByVal b As Node) As Boolean
Return Math.Abs(a.getX() - b.getX()) + Math.Abs(a.getY() - b.getY()) = 1
End Function
Public Function move(ByVal direction As Direction) As Node
Select Case direction
Case 0
upnode = New Node(getHead.getX, getHead.getY + 1)
body.Add(upnode)
Case 1
rightnode = New Node(getHead.getX + 1, getHead.getY)
body.Add(rightnode)
Case 2
downnode = New Node(getHead.getX, getHead.getY - 1)
body.Add(downnode)
Case 3
leftnode = New Node(getHead.getX - 1, getHead.getY)
body.Add(leftnode)
End Select
Return body.Item(-1) '尾部node未删去
End Function
Public Function getHead() As Node
Return Me.body.Item(1)
End Function
Public Function addTail(ByVal area As Node) As Node
Me.body.Add(area)
Return area
End Function
Public Function getBody() As Collection
Return Me.body
End Function
End Class


但是出现NullReferenceException问题

但是关键是
为什么会是nothing

却不是
...全文
255 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zj_zwl 2017-03-26
  • 打赏
  • 举报
回复
向上一级一级找,应该不难吧

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧