private void testComm()
{
for(short i=10;i>0;i--)
{
axMSComm1.CommPort=i;

try
{
axMSComm1.PortOpen=true;
}
catch(Exception ex) 
{
MessageBox.Show(ex.Message);
return;


// ;

if(axMSComm1.PortOpen==true)
{ Cbbox_Port.Items.Add("COM"+i);
axMSComm1.PortOpen=false;
Cbbox_Port.Text="COM"+i;
} } }
我在我机器调试的时候出现未处理的
“System.NullReferenceException”类型的异常出现在 WindowsApplication4.exe 中。其他信息: 未将对象引用设置到对象的实例。 请教这个问题这么解决,谢谢

解决方案 »

  1.   

    到底是哪一行出错?根据代码判断是axMSComm1没有初始化。
      

  2.   

    可能对象没有实例化:
    最好,在ACTIVEX中添加后,在引用中加入该ocx;
    在对象库中可以看到该对象的接口、命名空间等
              using MSCommLib;//命名空间
              ……
     protected MSCommLib.MSCommClass test1=new MSCommClass();//实例化
              ……
     test1.PortOpen=true;//使用
              test1.Output="dasfdsaf";
     test1.PortOpen=false;