Problem solve Get help with specific problems with your technologies, process and projects.

Extending simple performance counter retrieval to array

I tried to extend simple performance counter retrieval to array. It is OK when I build the code, but I got the following message pointing to perfCounter(loopCount) = New PerformanceCounter(tmpCs(0), tmpCs(1), tmpCs(2), "MYPC") when I debug it.

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

    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).

Dig Deeper on Win Development Resources

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.