首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

System.NullReferenceException: 未将对象引用设置到对象的实例,该怎么解决

2012-01-10 
System.NullReferenceException: 未将对象引用设置到对象的实例usingSystemusingSystem.DrawingusingSys

System.NullReferenceException: 未将对象引用设置到对象的实例
using   System;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;
using   System.Diagnostics;

namespace   proTracker1
{
///   <summary>
///   Form1   的摘要说明。
///   </summary>
public   class   Form1   :   System.Windows.Forms.Form
{
private   System.Windows.Forms.ListView   listView1;
private   System.Windows.Forms.ColumnHeader   columnHeader1;
private   System.Windows.Forms.ColumnHeader   columnHeader2;
private   System.Windows.Forms.ColumnHeader   columnHeader3;
private   System.Windows.Forms.ColumnHeader   columnHeader4;
private   System.Windows.Forms.ColumnHeader   columnHeader5;
private   System.Windows.Forms.ColumnHeader   columnHeader6;
private   System.Windows.Forms.Button   button1;
private   System.Windows.Forms.Button   button2;
private   System.Diagnostics.Process[]   procList=new   Process[100];
///   <summary>
///   必需的设计器变量。
///   </summary>
private   System.ComponentModel.Container   components   =   null;


public   Form1()
{
//
//   Windows   窗体设计器支持所必需的
//
InitializeComponent();
AddProcesses();

//
//   TODO:   在   InitializeComponent   调用后添加任何构造函数代码
//
}

///   <summary>
///   清理所有正在使用的资源。
///   </summary>
protected   override   void   Dispose(   bool   disposing   )
{
if(   disposing   )
{
if   (components   !=   null)  
{
components.Dispose();
}
}
base.Dispose(   disposing   );
}

#region   Windows   窗体设计器生成的代码
///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{
this.listView1   =   new   System.Windows.Forms.ListView();
this.columnHeader1   =   new   System.Windows.Forms.ColumnHeader();
this.columnHeader2   =   new   System.Windows.Forms.ColumnHeader();
this.columnHeader3   =   new   System.Windows.Forms.ColumnHeader();
this.columnHeader4   =   new   System.Windows.Forms.ColumnHeader();
this.columnHeader5   =   new   System.Windows.Forms.ColumnHeader();
this.columnHeader6   =   new   System.Windows.Forms.ColumnHeader();
this.button1   =   new   System.Windows.Forms.Button();
this.button2   =   new   System.Windows.Forms.Button();
this.SuspendLayout();
//  
//   listView1
//  
this.listView1.Columns.AddRange(new   System.Windows.Forms.ColumnHeader[]   {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6});
this.listView1.Dock   =   System.Windows.Forms.DockStyle.Top;
this.listView1.Location   =   new   System.Drawing.Point(0,   0);


this.listView1.Name   =   "listView1 ";
this.listView1.Size   =   new   System.Drawing.Size(528,   320);
this.listView1.TabIndex   =   0;
this.listView1.View   =   System.Windows.Forms.View.Details;
//  
//   columnHeader1
//  
this.columnHeader1.Text   =   "印象名称 ";
//  
//   columnHeader2
//  
this.columnHeader2.Text   =   "PID ";
//  
//   columnHeader3
//  
this.columnHeader3.Text   =   "CPU ";
//  
//   columnHeader4
//  
this.columnHeader4.Text   =   "优先级 ";
//  
//   columnHeader5
//  
this.columnHeader5.Text   =   "内存使用 ";
//  
//   columnHeader6
//  
this.columnHeader6.Text   =   "虚拟内存 ";
//  
//   button1
//  
this.button1.Location   =   new   System.Drawing.Point(104,   344);
this.button1.Name   =   "button1 ";
this.button1.TabIndex   =   1;
this.button1.Text   =   "btnkill ";
this.button1.Click   +=   new   System.EventHandler(this.button1_Click);
//  
//   button2
//  
this.button2.Location   =   new   System.Drawing.Point(264,   344);
this.button2.Name   =   "button2 ";
this.button2.TabIndex   =   2;
this.button2.Text   =   "btnRefresh ";
this.button2.Click   +=   new   System.EventHandler(this.button2_Click);
//  
//   Form1
//  
this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);
this.ClientSize   =   new   System.Drawing.Size(528,   373);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listView1);
this.Name   =   "Form1 ";
this.Text   =   "Form1 ";
this.ResumeLayout(false);

}
#endregion

///   <summary>
///   应用程序的主入口点。
///   </summary>
[STAThread]
static   void   Main()  
{
Application.Run(new   Form1());
}

private   void   button1_Click(object   sender,   System.EventArgs   e)
{
if(listView1.SelectedItems.Count <0)
{
MessageBox.Show( "Invalid   Selection ");
return;
}
for(int   i=listView1.SelectedItems.Count-1;i> =0;   i--)
{
ListViewItem   li=listView1.SelectedItems[i];
for(int   j=0;j <procList.Length;j++)
{
if(procList[j].ProcessName==li.Text)
{
procList[j].Kill();
}
}
listView1.Items.Remove(li);
}

}

public   void   InitializeListView()
{
this.listView1.Columns.AddRange(new   System.Windows.Forms.ColumnHeader[]
      {
      this.columnHeader1,
      this.columnHeader2,
      this.columnHeader3,
      this.columnHeader4,
      this.columnHeader5,
      this.columnHeader6,


});
this.listView1.ForeColor=System.Drawing.SystemColors.WindowText;

this.listView1.FullRowSelect=true;
this.listView1.GridLines=true;
this.listView1.Location=new   System.Drawing.Point(10,19);
this.listView1.Name= "listView1 ";
this.listView1.Size=new   System.Drawing.Size(615,277);
this.listView1.Sorting=System.Windows.Forms.SortOrder.Ascending;
                        this.listView1.TabIndex=0;
this.listView1.TabStop=false;
this.listView1.View=System.Windows.Forms.View.Details;
//this.listView1.KeyDown+=new   System.Windows.Forms.KeyEventHandler(this.ListView1_KeyDown);
this.columnHeader1.Text= "印象名称 ";
        this.columnHeader2.Text= "PID ";
this.columnHeader3.Text= "CPU ";
this.columnHeader4.Text= "优先级 ";
this.columnHeader5.Text= "内存使用 ";
this.columnHeader6.Text= "虚拟内存 ";
try
{
procList=Process.GetProcesses();
}
catch   (Exception   e)
{
MessageBox.Show(e.Message);
}


}


protected   void   AddProcesses()
{
      int   iVirtual,iPhysical;
for(int   i=0;i <procList.Length;i++)
{
iPhysical=procList[i].WorkingSet/1024;
    iVirtual=procList[i].VirtualMemorySize/1024;
listView1.Items.Add(new   ListViewItem(new   String   []   {
                                      procList[i].ProcessName,procList[i].Id.ToString(),procList[i].TotalProcessorTime.ToString(),
      procList[i].BasePriority.ToString(),iPhysical.ToString()+ "K ",iVirtual.ToString()+ "K "}
    )   );
}
}

private   void   button2_Click(object   sender,   System.EventArgs   e)
{
listView1.Clear();
}
}
}


我调试了很久,也上网查了下,但还是没能解决
显示的是System.NullReferenceException:   未将对象引用设置到对象的实例
哪位大虾能帮点拨下偶~~在下感激不尽~~~

[解决办法]
在调用 AddProcesses()加上这句

procList = Process.GetProcesses();
AddProcesses();

另获取procList[i].TotalProcessorTime可能发生违例

热点排行