如何处理异常“对象引用未设置为对象的实例“ ? [英] How to handle the exception" object reference not set to an instance of an object" ?

查看:67
本文介绍了如何处理异常“对象引用未设置为对象的实例“ ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Drawing;
使用 System.Collections;
使用 System.ComponentModel;
使用 System.Windows.Forms;
使用 System.Data;

使用 TAPI3Lib;

命名空间完成
{
/// < 摘要 >
/// Form1的摘要说明。
/// < / summary >
public class Form1:System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
/// < 摘要 >
/// 必需的设计变量。
/// < / summary >
private System.ComponentModel.Container components = null ;


private TAPIClass tapi;
private callnotification call_notify;
私有 注册;
public static ITAddress call_address;

public ITBasicCallControl call_control;

public ITCallHub Call_Hub;


public Form1()
{
/ /
// 必填for Windows Form Designer支持
//
InitializeComponent() ;

bool found = false ;

tapi = new TAPIClass();
tapi.Initialize();

call_notify = new callnotification();
call_notify.addtolist = new callnotification.listshow( this .status);
tapi.ITTAPIEventNotification_Event_Event + = new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(call_notify.Event);
tapi.EventFilter =( int )(TAPI_EVENT.TE_CALLNOTIFICATION |
TAPI_EVENT.TE_DIGITEVENT |
TAPI_EVENT.TE_PHONEEVENT |
TAPI_EVENT.TE_CALLSTATE |
TAPI_EVENT.TE_GENERATEEVENT |
TAPI_EVENT.TE_GATHERDIGITS |
TAPI_EVENT.TE_REQUEST);


ITCollection collec;
ITAddress地址;
ITMediaSupport支持;
ITAddressCapabilities功能;
collec =(ITCollection)tapi.Addresses;
foreach (ITAddress addr in collec)
{

found = false ;
address = addr;
support =(ITMediaSupport)地址;
capability =(ITAddressCapabilities)地址;
if (support.QueryMediaType(TapiConstants.TAPIMEDIATYPE_AUDIO))
{
found = ;
}
capability = null ;
support = null ;
address = null ;
if (found == true
{
if (addr.AddressName.ToUpper()== H323 LINE
{

call_address = addr;
}
// break;
}
}

/// / registration part
// 这里我得到了例外。
registration = tapi.RegisterCallNotifications(call_address, true true ,TapiConstants.TAPIMEDIATYPE_AUDIO, 1 );


}

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

#region Windows窗体设计器生成的代码
/// < 摘要 >
/// Designer支持所需的方法 - 不要修改
/// 此方法的内容使用代码编辑器。
/// < / summary >
private void InitializeComponent()
{
this .listBox1 = new System.Windows.Forms.ListBox();
this .textBox1 = new System.Windows.Forms.TextBox();
this .button1 = new System.Windows.Forms.Button();
this .button2 = new System.Windows.Forms.Button();
this .button3 = new System.Windows.Forms.Button();
this .label2 = new System.Windows.Forms.Label();
this .label3 = new System.Windows.Forms.Label();
this .SuspendLayout();
//
// listBox1
//
this .listBox1.ItemHeight = 14 ;
this .listBox1.Location = new System.Drawing.Point( 16 128 );
this .listBox1.Name = listBox1 ;
this .listBox1.Size = new System.Drawing.Size( 200 102 );
this .listBox1.TabIndex = 0 ;
//
// textBox1
//
this .textBox1.Location = new System.Drawing.Point( 72 16 );
this .textBox1.Name = textBox1 ;
this .textBox1.Size = new System.Drawing.Size( 216 20 );
this .textBox1.TabIndex = 1 ;
this .textBox1.Text = 127.0 .0.1\" ;
//
// button1
//
this .button1.Location = new System.Drawing.Point( 160 64 );
this .button1.Name = button1 ;
this .button1.Size = new System.Drawing.Size( 120 32 );
this .button1.TabIndex = 2 ;
this .button1.Text = 致电;
this .button1.Click + = new System.EventHandler( .button1_Click);
//
// button2
//
this .button2.Location = new System.Drawing.Point( 32 64 );
this .button2.Name = button2 ;
this .button2.Size = new System.Drawing.Size( 120 32 );
this .button2.TabIndex = 3 ;
this .button2.Text = 断开连接;
this .button2.Click + = new System.EventHandler( .button2_Click);
//
// button3
//
this .button3.Location = new System.Drawing.Point( 224 200 );
this .button3.Name = button3 ;
this .button3.TabIndex = 4 ;
this .button3.Text = 清除状态;
this .button3.Click + = new System.EventHandler( .button3_Click);
//
// label2
//
this .label2.BackColor = System.Drawing.Color.Transparent;
this .label2.ForeColor = System.Drawing.Color.Black;
this .label2.Location = new System.Drawing.Point( 8 16 );
this .label2.Name = label2 ;
this .label2.Size = new System.Drawing.Size( 64 16 );
this .label2.TabIndex = 6 ;
this .label2.Text = IP地址;
//
// label3
//
this .label3.BackColor = System.Drawing.Color.Transparent;
this .label3.ForeColor = System.Drawing.Color.Black;
this .label3.Location = new System.Drawing.Point( 16 104 );
this .label3.Name = label3 ;
this .label3.Size = new System.Drawing.Size( 104 24 );
this .label3.TabIndex = 7 ;
this .label3.Text = 致电状态;
//
// Form1
//
this .AutoScaleBaseSize = new System.Drawing.Size( 5 13 );
this .ClientSize = new System.Drawing.Size( 330 239 );
this .Controls.Add( this .label3);
this .Controls.Add( this .label2);
this .Controls.Add( this .button3);
this .Controls.Add( this .button2);
this .Controls.Add( this .button1);
this .Controls.Add( this .textBox1);
this .Controls.Add( this .listBox1);
this .FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
.MaximizeBox = false ;
this .Name = Form1< /跨度>;
this .StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
.Text = 音频会议 - www.fadidotnet.org;
this .ResumeLayout( false );

}
#endregion

private void status( string str)
{
listBox1 .Items.Add(STR);
}

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

private void button1_Click( object sender,System.EventArgs e)
{
string addr = textBox1.Text;
MessageBox.Show( 拨打 + addr + ...);
// public call_control = new call_address.CreatCall(addr,TapiConstants.LINEADDRESSTYPE_IPADDRESS,TapiConstants.TAPIMEDIATYPE_AUDIO | TapiConstants。 TAPIMEDIATYPE_VIDEO);
call_control = call_address.CreateCall(addr,TapiConstants.LINEADDRESSTYPE_IPADDRESS,TapiConstants.TAPIMEDIATYPE_AUDIO | TapiConstants.TAPIMEDIATYPE_VIDEO);
button1.Enabled = false ;
IEnumStream enum_stream;
ITStreamControl pstream_control;
pstream_control =(ITStreamControl)call_control;
pstream_control.EnumerateStreams( out enum_stream);
ITStream p_stream;
uint a11 = 0 ;
enum_stream.Next( 1 out p_stream,参考 a11);
int imedia;
imedia = p_stream.MediaType;
TERMINAL_DIRECTION目录;
dir = p_stream.Direction;
ITTerminal termi,termi1;

ITTerminalSupport term_support =(ITTerminalSupport)call_address;
termi = term_support.GetDefaultStaticTerminal(imedia,dir);
p_stream.SelectTerminal(termi);



enum_stream.Next( 1 out p_stream, ref a11);
termi1 = term_support.GetDefaultStaticTerminal(imedia,TERMINAL_DIRECTION.TD_CAPTURE);
p_stream.SelectTerminal(termi1);
call_control.Connect( false );

}

private void button2_Click(< span class =code-keyword> object
sender,System.EventArgs e)
{
call_control.Disconnect(DISCONNECT_CODE.DC_NORMAL);
button1.Enabled = true ;
}

private void button3_Click( object sender,System.EventArgs e)
{
listBox1.Items.Clear();
}
}
}





我尝试了什么:



i尝试使用异常处理(尝试并捕获)。但这并没有完全解决问题。

解决方案

< blockquote>一般来说?找到抛出异常的位置(在哪个对象上),并确保在使用它之前创建该对象的实例



你没有给我们足够的信息来帮助我们你 - 当我这样做时,这种情况发生了吹嘘描述 - 请使用调试器,逐步执行代码并找出程序爆炸/抛出异常的位置和/或查看异常数据以识别对象

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using TAPI3Lib;

namespace complete
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		private TAPIClass tapi;
		private callnotification call_notify;
		private long registration;
		public static ITAddress call_address;
				
		public ITBasicCallControl call_control;

		public ITCallHub  Call_Hub;


		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			bool found=false;

			tapi=new TAPIClass();
			tapi.Initialize();
            
			call_notify=new callnotification();
			call_notify.addtolist=new callnotification.listshow(this.status);
			tapi.ITTAPIEventNotification_Event_Event+=new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(call_notify.Event);
			tapi.EventFilter=(int)(TAPI_EVENT.TE_CALLNOTIFICATION|
				TAPI_EVENT.TE_DIGITEVENT|
				TAPI_EVENT.TE_PHONEEVENT|
				TAPI_EVENT.TE_CALLSTATE|
				TAPI_EVENT.TE_GENERATEEVENT|
				TAPI_EVENT.TE_GATHERDIGITS|
				TAPI_EVENT.TE_REQUEST);


			ITCollection collec;
			ITAddress address;
			ITMediaSupport support;
			ITAddressCapabilities capability;
			collec=(ITCollection)tapi.Addresses;
			foreach(ITAddress addr in collec )
			{

				found=false;
				address=addr;
				support=(ITMediaSupport)address;
				capability=(ITAddressCapabilities)address;
				if(support.QueryMediaType(TapiConstants.TAPIMEDIATYPE_AUDIO))
				{
					found=true;
				}
				capability=null;
				support=null;
				address=null;
				if(found==true)
				{
					if(addr.AddressName.ToUpper()=="H323 LINE")
					{

						call_address=addr;
					}
					//break;
				}
			}

			////registration part
           // here i'm getting the exception.
                registration = tapi.RegisterCallNotifications(call_address, true, true, TapiConstants.TAPIMEDIATYPE_AUDIO, 1);
           

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 14;
			this.listBox1.Location = new System.Drawing.Point(16, 128);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(200, 102);
			this.listBox1.TabIndex = 0;
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(72, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(216, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "127.0.0.1";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(160, 64);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(120, 32);
			this.button1.TabIndex = 2;
			this.button1.Text = "Call";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(32, 64);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(120, 32);
			this.button2.TabIndex = 3;
			this.button2.Text = "Disconnect";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(224, 200);
			this.button3.Name = "button3";
			this.button3.TabIndex = 4;
			this.button3.Text = "Clear Status";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// label2
			// 
			this.label2.BackColor = System.Drawing.Color.Transparent;
			this.label2.ForeColor = System.Drawing.Color.Black;
			this.label2.Location = new System.Drawing.Point(8, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "IP Address";
			// 
			// label3
			// 
			this.label3.BackColor = System.Drawing.Color.Transparent;
			this.label3.ForeColor = System.Drawing.Color.Black;
			this.label3.Location = new System.Drawing.Point(16, 104);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(104, 24);
			this.label3.TabIndex = 7;
			this.label3.Text = "Call Status";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(330, 239);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.listBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Audio Conferencing - www.fadidotnet.org";
			this.ResumeLayout(false);

		}
		#endregion

		private void status(string str)
		{
			listBox1.Items.Add(str);
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			string addr=textBox1.Text;
			MessageBox.Show("Dialing to "+addr+" ...");
            //public call_control=new call_address.CreatCall(addr,TapiConstants.LINEADDRESSTYPE_IPADDRESS,TapiConstants.TAPIMEDIATYPE_AUDIO | TapiConstants.TAPIMEDIATYPE_VIDEO);
			call_control=call_address.CreateCall(addr, TapiConstants.LINEADDRESSTYPE_IPADDRESS,TapiConstants.TAPIMEDIATYPE_AUDIO | TapiConstants.TAPIMEDIATYPE_VIDEO);
			button1.Enabled=false;
            IEnumStream enum_stream ;
			ITStreamControl pstream_control;
			pstream_control=(ITStreamControl)call_control;
			pstream_control.EnumerateStreams(out enum_stream);
			ITStream p_stream;
			uint a11=0;
			enum_stream.Next(1,out p_stream,ref a11);
			int imedia;
			imedia=p_stream.MediaType;
			TERMINAL_DIRECTION dir;
			dir=p_stream.Direction;
			ITTerminal termi,termi1;
				
			ITTerminalSupport term_support=(ITTerminalSupport)call_address;
			termi=term_support.GetDefaultStaticTerminal(imedia,dir);
			p_stream.SelectTerminal(termi);

			

			enum_stream.Next(1,out p_stream,ref a11);
			termi1=term_support.GetDefaultStaticTerminal(imedia,TERMINAL_DIRECTION.TD_CAPTURE);
			p_stream.SelectTerminal(termi1);
			call_control.Connect(false);

		}

		private void button2_Click(object sender, System.EventArgs e)
		{
		       call_control.Disconnect(DISCONNECT_CODE.DC_NORMAL);
			   button1.Enabled=true;
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
		       listBox1.Items.Clear();
		}
	}
}



What I have tried:

i tried using exception handling(try and catch).but that does not solve the issue completely.

解决方案

in general terms ? find where the exception is being thrown (on which object), and ensure an instance of that object is created before you use it

you dont give us enough information to help you - the "when I do this, this happens" blow by blow description - please use the debugger, step through the code and find out where your program blows up/throws the exception and/or look at the exception data to identify the object


这篇关于如何处理异常“对象引用未设置为对象的实例“ ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆