问题描述
使用委托事件的时候,报“未将对象引用到实列”usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMyDZCZ.cls;usingMyDZCZ.UI.CarManager;usingSystem.Threading;namespaceMyDZCZ.baseInfor{publicdelegatevoidPassStrHandel(stringstr);publicpartialclassProductsGoodsFrm:DevExpress.XtraEditors.XtraForm{publiceventPassStrHandelpssStr;privateboolIsMouseDown=false;privatePointmouseOffset;publicProductsGoodsFrm(){InitializeComponent();}DataSetds;stringstrSql;privatevoidProductsGoodsFrm_Load(objectsender,EventArgse){ShowData();}///<summary>///加载数据///</summary>publicvoidShowData(){clsBase.dbHelper=newdbHelp(clsBase.ConnectiongString());strSql="select*fromtblProds";if(clsBase.dbHelper.ConnDB()){using(ds=clsBase.dbHelper.ExcuteSelect(strSql,"tblProds")){for(inti=0;i<ds.Tables[0].Rows.Count;i++){DataRowdr=ds.Tables[0].Rows[i];intindex=this.dataGridView1.Rows.Add();this.dataGridView1.Rows[index].Cells[0].Value=dr[1].ToString();this.dataGridView1.Rows[index].Cells[1].Value=dr[2].ToString();this.dataGridView1.Rows[index].Cells[2].Value=dr[3].ToString();this.dataGridView1.Rows[index].Cells[3].Value=dr[4].ToString();}}}}privatevoidbtn_Esc_Click(objectsender,EventArgse){this.Close();}privatevoidsplitContainer1_Panel1_MouseDown(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){IsMouseDown=true;}mouseOffset=newPoint(-e.X,-e.Y);}privatevoidsplitContainer1_Panel1_MouseMove(objectsender,MouseEventArgse){if(IsMouseDown==true){PointmousePos=Control.MousePosition;mousePos.Offset(mouseOffset.X,mouseOffset.Y);this.Location=mousePos;}}privatevoidsplitContainer1_Panel1_MouseUp(objectsender,MouseEventArgse){IsMouseDown=false;}privatevoidbtn_Sure_Click(objectsender,EventArgse){if(this.txt_Products.Text.Trim()!=""&&this.txt_Products.Text.Trim()!=null){if(pssStr!=null)pssStr(txt_Products.Text.Trim());//这个地方报错空异常??请问这里为什么会是null???????????this.txt_Products.Text="";}}privatevoiddataGridView1_DoubleClick(objectsender,EventArgse){this.txt_Products.Text=this.dataGridView1.CurrentRow.Cells[0].Value.ToString();//pssStr(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());}}}````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````父窗体usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMyDZCZ.baseInfor;namespaceMyDZCZ.UI.CarManager{publicpartialclassEditeFrm:Form{publicEditeFrm(){InitializeComponent();Control.CheckForIllegalCrossThreadCalls=false;}#region属性privatestringcarNum;///<summary>///车号///</summary>publicstringCarNum{get{returncarNum;}set{carNum=value;}}privatestringdriveName;///<summary>///司机姓名///</summary>publicstringDriveName{get{returndriveName;}set{driveName=value;}}privatestringproductsName;///<summary>///货物///</summary>publicstringProductsName{get{returnproductsName;}set{productsName=value;}}privatestringproductSize;///<summary>///货物规格///</summary>publicstringProductSize{get{returnproductSize;}set{productSize=value;}}privatestringsupplyUnit;///<summary>///供货单位///</summary>publicstringSupplyUnit{get{returnsupplyUnit;}set{supplyUnit=value;}}privatestringreciveUnit;///<summary>///收货单位///</summary>publicstringReciveUnit{get{returnreciveUnit;}set{reciveUnit=value;}}privatestringtrafficUnit;///<summary>///运输单位///</summary>publicstringTrafficUnit{get{returntrafficUnit;}set{trafficUnit=value;}}#endregionProductsGoodsFrmPF;privatevoidEditeFrm_Load(objectsender,EventArgse){Cf=newchooseCarNumFrm();Cf.PassString+=newPassStringHander(Cf_PassString);PF=newProductsGoodsFrm();PF.pssStr+=newPassStrHandel(PF_pssStr);this.txt_CarNo.Text=this.CarNum;this.txt_Drive.Text=this.DriveName;this.txt_Size.Text=this.ProductSize;this.userControl11.TxtBox=this.ProductsName;this.userControl12.TxtBox=this.SupplyUnit;this.userControl13.TxtBox=this.ReciveUnit;this.userControl14.TxtBox=this.TrafficUnit;this.userControl11.pastr+=newMyDZCZ.Controls.PssstrHandel(userControl11_pastr);this.userControl12.pastr+=newMyDZCZ.Controls.PssstrHandel(userControl12_pastr);this.userControl13.pastr+=newMyDZCZ.Controls.PssstrHandel(userControl13_pastr);this.userControl14.pastr+=newMyDZCZ.Controls.PssstrHandel(userControl14_pastr);}///<summary>///委托过来的执行事件///</summary>///<paramname="str"></param>voidPF_pssStr(stringstr){if(str!="")this.userControl11.TxtBox=str;}#region事件voiduserControl14_pastr(){}voiduserControl13_pastr(){}voiduserControl12_pastr(){}voiduserControl11_pastr(){ProductsGoodsFrmpf=newProductsGoodsFrm();Pointpostion=System.Windows.Forms.Cursor.Position;pf.Left=postion.X;pf.Top=postion.Y;if(pf.ShowDialog()==System.Windows.Forms.DialogResult.OK){return;}}#endregionvoidCf_PassString(stringstr){if(str!="")this.txt_CarNo.Text=str;}chooseCarNumFrmCf;privatevoidbtn_SelectCarNo_Click(objectsender,EventArgse){Pointpostion=System.Windows.Forms.Cursor.Position;Cf.Left=postion.X;Cf.Top=postion.Y;Cf.ShowDialog();}}}
解决方案
解决方案二:
不看了,错误提示告诉你了,访问量空对象,看哪行出错,保证使用前已经创建对象
解决方案三:
http://bbs.csdn.net/topics/360140208
解决方案四:
未将对象引用到实列只要出现了这个,保证是某个变量为null,你却要访问它的属性
解决方案五:
pssStr(txt_Products.Text.Trim())这是啥用法你把event当function来用吗
解决方案六:
该new的地方没new
解决方案七:
太长了,看着累。你设置断点调试,看看哪个对象没实例化。
解决方案八:
是挺奇怪的if(this.txt_Products.Text.Trim()!=""&&this.txt_Products.Text.Trim()!=null){if(pssStr!=null)pssStr(txt_Products.Text.Trim());//代码能执行到这里,说明txt_Products.Text.Trim()不是空,事件pssStr也绑定了this.txt_Products.Text="";}
解决方案九:
另外,发现一个问题,你在父窗体里new了ProductsGoodsFrm,也注册了事件,唯独没有show子窗体PF=newProductsGoodsFrm();PF.pssStr+=newPassStrHandel(PF_pssStr);//...PF.Show();//在代码段的最后加上这句
解决方案十:
PF=newProductsGoodsFrm();PF.pssStr+=newPassStrHandel(PF_pssStr);//...PF.Show();//在代码段的最后加上这句我没必要SHOW啊,我只是在子窗体点击Button按钮的时候传递一个string字符串给父窗体啊,但是事件却为null是怎么回事?
解决方案十一:
既然你能断点调试,不要告诉我这个地方报空异常到底什么变量是空??
解决方案十二:
太长了,看着累。