-1

Why won't this code add an element to the array "numbers"?

   Dim numbers() As Double

   Dim counter As Integer

   Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

      Const BASE As Double = 200      'base pay
      Const COMM As Double = 1.09     'commission rate

      Dim gross As Double = txtSalesAmount.Text     'user input (gross sales)
      Dim pay As Double = BASE + (gross * COMM)     'calculates total pay


      numbers(counter) = pay
      counter += 1
      txtSalesAmount.Text = ""

All I get is the dreaded exception:

An unhandled exception of type 'System.NullReferenceException' occurred in Salary_Summary.exe

Additional information: Object reference not set to an instance of an object.

As you can tell I am a complete novice.....sorry if this is trite.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
user3500004
  • 17
  • 1
  • 3
  • -1 You need to use the debugger to see exactly what and why is causing an error. – John Alexiou Apr 04 '14 at 23:55
  • Well, I can't initialize it because I don't know the element values until the user inputs them. In the same vein, I don't know how many elements will be added: it could be 3, it could be 23 or 123. Ultimately, I will need to do a frequency array on the numbers array, but there are no elements in the numbers array until the user inputs them. – user3500004 Apr 04 '14 at 23:57
  • the debugger stops at the numbers(counter) = pay line. I don't know enough about debugger to go beyond that. – user3500004 Apr 04 '14 at 23:59
  • Yep, you initialize an array and it fills it with zeros. Only then you can go and change the values to what you want. – John Alexiou Apr 04 '14 at 23:59
  • And if you put your cursor over the `numbers` variable you will see it has the value of `Nothing` (or `null`). – John Alexiou Apr 05 '14 at 00:00
  • And then a simple google search lands you in: http://msdn.microsoft.com/en-us/library/y13tek7e.aspx – John Alexiou Apr 05 '14 at 00:01
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 05 '14 at 00:03

2 Answers2

1

You arent initialing the array. Also, arrays are fixed in size from construction. I would suggest using a List(Of Double).

Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
1
  1. Dim gross As Double = txtSalesAmount.Text is wrong, as you need to convert the text to a number, and I suggest using Decimal with currency.
  2. numbers() is never initialized to any size. Arrays in .NET are of fixed size, and need to be initialized for a size. Example Dim numbers as Double() = New Double(100) { }
  3. Arrays can be resized using the Array.Resize() function to fit more elements
  4. If you are going to be adding elements, use a List(Of ) which internally declares an array, but it handles all the bookkeeping and resizing internally.
  5. As the error says, the array has not been initialized, and array variables are references which default to Nothing.
John Alexiou
  • 23,931
  • 6
  • 67
  • 123
  • OK, converting the text string to a double has it working as best as I can tell... I need to build the frequency array now to see if the numbers array is actually being populated... – user3500004 Apr 05 '14 at 00:03