开发者

C# Performance Counter Object reference not set to an instance to an object [duplicate]

This question already has answers here: What is a NullReferenceException, and how do I fix it? (27 answers) Closed 6 years ago.

I was coding on my project and wanted to debug it, but I got this error message:

Object reference not set to an instance to an object

NullReferenceExpection was unhandled

My program was working and I had this error for a few days. There I solved the problem with, I think, with typing something in my wi开发者_Python百科ndows console. I think I reseted the Performance Counter and then it worked.

Here is the code:

public void Counter()
{
    counter = new PerformanceCounter();
    counter.CategoryName = "Processor";
    counter.CounterName = "% Processor Time";
    counter.InstanceName = "_Total";
}

public double GetCounter()
{
    return counter.NextValue();  //error message is in this line
}


It is very difficult to help you without more information but I will try to tell you how to figure out the problem yourself.

You should look in the stack trace to get the line of code that threw the error. This should show you what is going wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜