0

I always get NullReferenceException error when I try to run the this code:

Dim startx As Int64
Dim starty As Int64
Dim count As Int64 = 0
Dim Position() As Point
startx = 15
starty = 18
Position(count) = New Point(startx, starty)

Can someone tell me why this doesn't work?

Martin
  • 16,093
  • 1
  • 29
  • 48
Rnd661
  • 1
  • You never initilized `Position` (e.g., `Dim Position() As Point = New Point(10) {}`). Plus, see [Point(Int32, Int32)](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.point.-ctor?view=netframework-4.8#System_Drawing_Point__ctor_System_Int32_System_Int32_). Declare all your variables as `Integer` (`Int32`) types. – Jimi Nov 29 '19 at 12:25
  • Thank you I am new to .net and I didn't know that. – Rnd661 Nov 29 '19 at 12:35

1 Answers1

0

Position is declared as an array with 0 items in it. On the last line, you are trying to set the first item (indexed 0), but since there are no items in the array yet, it will fail.

One solution is to declare the array having one item:

Dim Position(0) As Point

Or, resize the array when needed

Dim Position() As Point    

... other code ...

ReDim Preserve Position(0)
Berend
  • 780
  • 7
  • 19