Q

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

Dig deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close