0

I'm getting a Null Reference error with a Looped Array. I believe I need to create a new instance but can't figure out syntax.

Here is the Public declaration:

Public fbInitial() As Double
Public bmInitial() As Double
Public dpInitial() As Double

Here is the code that throws the Null error:

' Calculate initial values and write text file header
If x = 1 Then
    fbInitial(source) = FormatNumber(detPwrAvg / backMonAvg, 6)     'Sets initial SLD Front/Back ratio on first sample                    
    bmInitial(source) = FormatNumber(backMonAvg, 6)                 'Sets initial Back Monitor on first sample    
    dpInitial(source) = FormatNumber(detPwrAvg, 6)                  'Sets initial Detector Power on first sample    
    file.WriteLine("Sample;Intentionally Blank;Chamber Temp;Intentionally Blank;Back Monitor (uA);Laser Drive (mA);Intentionally Blank;Detector Power (uW);Fwd/Back Ratio;Normalized F/B Ratio;Normalized Back Monitor;Normalized Fwd Power")
End If
John Saunders
  • 157,405
  • 24
  • 229
  • 388
HurstOlds
  • 51
  • 1
  • 7
  • 3
    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 May 14 '14 at 01:21
  • Also, FYI, it's not an error. It's an exception. I strongly suggest that you learn the difference. See http://msdn.microsoft.com/en-us/library/s6da8809%28v=vs.100%29.aspx – John Saunders May 14 '14 at 01:24
  • Also see http://msdn.microsoft.com/en-us/library/system.exception.aspx. – John Saunders May 14 '14 at 01:30
  • I cant see what where the loop comes into play, but with the little bit of code there, those array are just declared, and are still nothing unless they are initialized in other code. e.g. `Public fbInitial(6) As Double`, or `ReDim` Since they look related, they might be better off as properties in a class. One way to avoid the hassles of an array is to use a List(Of T) instead or an ArrayList instead. – Ňɏssa Pøngjǣrdenlarp May 14 '14 at 01:51
  • Plutonix... initializing the array worked perfectly. I will read up on your suggestions... thanks! – HurstOlds May 14 '14 at 01:57
  • JS - Started reading your links to better educate myself. Interestingly the first statement is "Visual Basic supports structured exception (error) handling, which allows the program to detect and possibly recover from errors during execution." I will keep reading... thanks. – HurstOlds May 14 '14 at 02:01

1 Answers1

0

It means that you need to specify the size of the variable array like:

Public fbInitial(1000000) As Double
Public bmInitial(1000000) As Double
Public dpInitial(1000000) As Double
GoroundoVipa
  • 257
  • 3
  • 11