An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication1.exe.
Additional information: Object reference not set to an instance of an object.
Any idea why this is happening? Thanks in advance. The code is as follows:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Module Module1
Sub Main()
Dim perfCounters As String()
Dim tmpC As String
Dim tmpCs As String()
Dim perfCounter As PerformanceCounter()
Dim loopCount As Integer
perfCounters = New String() {"Processor\% Processor Time\_Total", _
"Processor\% User Time\_Total"}
'perfCounter = New PerformanceCounter() {}
loopCount = 0
For Each tmpC In perfCounters
loopCount += 1
tmpCs = Split(tmpC, "\\")
perfCounter(loopCount) = New PerformanceCounter(tmpCs(0), tmpCs
(1), tmpCs(2), "MYPC")
Next tmpC
For loopCount = 1 To perfCounters.Length
Debug.WriteLine(perfCounter(loopCount).NextValue.ToString())
Next
'perfCounter.Close()
End Sub
End Module
You failed to initialize the perfCounter array. The NullReference exception is when you try to assign the returned PerformanceCounter class to perfCounter(loopCount).
This was first published in April 2004