public  void btnTakePhotos_Click(object sender, EventArgs e)    //截图
           {
               btnTakePhotos.Text = "抓拍";
               
               RobotEngine2.TakePhoto(cameraWindow.Camera.LastFrame, ImagePath, CreatePictureFile());
           }
红字的这段,在调试的时候弹出“未处理NullReferenceException”
而且我RobotEngine2为null
而我看别人的程序是RobotEngine2为{WIFIRobotCMDEngineV2.WifiRobotCMDEngineV2}
请问我要怎么才能不为null?
WIFIRobotCMDEngineV2是引入的dll控件,我在引用中已经引用进去了,也用了using了,
而且也public WifiRobotCMDEngineV2 RobotEngine2;//实例化引擎了
求救啊!!!

解决方案 »

  1.   

    我觉得TakePhoto应该不是静态方法,你没有实例化具体对象应该如下吧  RobotEngine2=new WifiRobotCMDEngineV2();//实例化对象
      RobotEngine2.TakePhoto(cameraWindow.Camera.LastFrame, ImagePath, CreatePictureFile());
      

  2.   

    代码好长呃。放出来好恶心呃。。
    我复制出来吧,求解救
    using WIFIRobotCMDEngineV2;
      static string ImagePath = Application.StartupPath + "\\Snapshot\\";//截图
     public WifiRobotCMDEngineV2 RobotEngine2;//实例化引擎/// ////////////////////////////////////////////////截图//////
                public  void camera_NewFrame(object sender, System.EventArgs e)
                {
                    if (saveOnMotion == true)
                    {
                        Camera camera = cameraWindow.Camera;
                        camera.Lock();
                        writer.AddFrame(camera.LastFrame);
                        camera.Unlock();
                    }
                    else
                    {
                        if (writer != null)
                        { 
                            writer.Dispose();
                            writer = null;
                        }                }
                }            public string CreatePictureFile()
                {
                    DateTime date = DateTime.Now;
                    string fileName = string.Format("{0}-{1}-{2} {3}-{4}-{5}.bmp",
                        date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
                    return fileName;
                }
                
            
               public  void btnTakePhotos_Click(object sender, EventArgs e)    //截图
               {
                   btnTakePhotos.Text = "抓拍";
                   
                   RobotEngine2.TakePhoto(cameraWindow.Camera.LastFrame, ImagePath, CreatePictureFile());
               }
    应该没有缺了,这是关于截图的
      

  3.   


    那你要看一下WifiRobotCMDEngineV2的构造函数是什么啊
      

  4.   

    光标放在括号中,然后快捷键 同时按ctr+shift+空格,查看构造函数的重载参数也可以选中括号前面的WifiRobotCMDEngineV2,即RobotEngine2=new WifiRobotCMDEngineV2();红色部分,右键->转到定义,查看构造函数
      

  5.   

    using System;
    using System.Drawing;
    using System.IO.Ports;
    using System.Net;
    using System.Net.Sockets;namespace WIFIRobotCMDEngineV2
    {
        public class WifiRobotCMDEngineV2
        {
            public WifiRobotCMDEngineV2(object cor);        public IPEndPoint IPE { get; set; }
            public Socket SOCKET { get; set; }        public event WifiRobotCMDEngineV2.SetRadarValue Setradarvalue;        public string byteToHexStr(byte[] bytes);
            public byte[] CreateData(byte type, byte cmd, byte data);
            public static string Domain2ip(string str);
            public void DrawSignal(int signalType, Graphics g, int x, int y, int PenWidth);
            public byte[] HexStringToByteArray(string s);
            public void SendCMD(int controlType, byte[] byteData, SerialPort comm);
            public void SendCMD(int controlType, string CMD_Custom, SerialPort comm);
            public void SendDataInComm(SerialPort comm, byte[] data);
            public void SendDataInComm(SerialPort comm, string data);
            public void SendHeartCMD(int controlType, SerialPort comm);
            public bool SocketConnect();
            public void TakePhoto(Bitmap snapshot, string RootPath, string FileName);        public delegate void SetRadarValue(string radarvalue);
        }
    }
    是这个吗?然后要怎么做呢?谢谢
      

  6.   

    没看到么,构造函数中包含一个参数,是object类型的   public WifiRobotCMDEngineV2(object cor);如果没有参数传递,可以传递为null,如下
    RobotEngine2=new WifiRobotCMDEngineV2(null);//实例化对象如果有参数,那么传递相应参数就可以,比如字符串
    RobotEngine2=new WifiRobotCMDEngineV2("参数");//实例化对象
      

  7.   

       public MainForm()
            {
                //
                // Required for Windows Form Designer support
                //
                InitializeComponent();
                RobotEngine2 = new WifiRobotCMDEngineV2((Object)this.statusBar); 
                //
                // TODO: Add any constructor code after InitializeComponent call
                //
            }
    是不是要加上这个?别人的程序里有这个东西,可是我打上去运行时有错误,”未处理ArgumentOutOfRangeException   索引超出范围,必须为非负值并小于集合大小。参数名:index。”是这句的问题吗?如果是缺这句的话改怎么解决?
      

  8.   

    我又有问题了,求救啊
                bool ret = false;
                private bool InitWIFISocket(String controlIp, String port)
                {
                    
                    this.Systemstatus.Text = "正在尝试连接WIFI板···";                try
                    {
                       RobotEngine2 = new WifiRobotCMDEngineV2(socket );
                       RobotEngine2 = new WifiRobotCMDEngineV2(ipe);
                        RobotEngine2 = new WifiRobotCMDEngineV2(ret);
                        RobotEngine2 = new WifiRobotCMDEngineV2(ips);
                        RobotEngine2 = new WifiRobotCMDEngineV2(controlIp);
                        RobotEngine2 = new WifiRobotCMDEngineV2(port );
                        RobotEngine2 = new WifiRobotCMDEngineV2(SocketType.Stream );
                        RobotEngine2 = new WifiRobotCMDEngineV2(ProtocolType.Tcp);
                        RobotEngine2 = new WifiRobotCMDEngineV2(AddressFamily.InterNetwork);
                        RobotEngine2   = new WifiRobotCMDEngineV2(RobotEngine2.IPE  );
                        RobotEngine2  = new WifiRobotCMDEngineV2(RobotEngine2 .SOCKET );                    ips = IPAddress.Parse(controlIp.ToString());
                        ipe = new IPEndPoint(ips, Convert.ToInt32(port.ToString()));
                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                    RobotEngine2.SOCKET = socket;
                        RobotEngine2.IPE = ipe;
                        ret = RobotEngine2.SocketConnect();                }
                    catch (Exception e)
                    {                    MessageBox.Show("WIFI初始化失败:" + e.Message, "WIFI初始化失败提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return ret;
                }
    我已经实例化了RobotEngine2.SOCKET和RobotEngine2.IPE了,为什么调试的时候我发现socket无法赋值给RobotEngine2.SOCKET,ipe无法赋值给RobotEngine2.IPE?而且RobotEngine2.SOCKET和RobotEngine2.IPE都为null,这是什么问题?
      

  9.   

      bool ret = false;
      private bool InitWIFISocket(String controlIp, String port)
      {
        
      this.Systemstatus.Text = "正在尝试连接WIFI板···";  try
      {
      RobotEngine2 = new WifiRobotCMDEngineV2(socket );
      RobotEngine2 = new WifiRobotCMDEngineV2(ipe);
      RobotEngine2 = new WifiRobotCMDEngineV2(ret);
      RobotEngine2 = new WifiRobotCMDEngineV2(ips);
      RobotEngine2 = new WifiRobotCMDEngineV2(controlIp);
      RobotEngine2 = new WifiRobotCMDEngineV2(port );
      RobotEngine2 = new WifiRobotCMDEngineV2(SocketType.Stream );
      RobotEngine2 = new WifiRobotCMDEngineV2(ProtocolType.Tcp);
      RobotEngine2 = new WifiRobotCMDEngineV2(AddressFamily.InterNetwork);
      RobotEngine2 = new WifiRobotCMDEngineV2(RobotEngine2.IPE );
      RobotEngine2 = new WifiRobotCMDEngineV2(RobotEngine2 .SOCKET );  ips = IPAddress.Parse(controlIp.ToString());
      ipe = new IPEndPoint(ips, Convert.ToInt32(port.ToString()));
      socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  RobotEngine2.SOCKET = socket;
      RobotEngine2.IPE = ipe;
      ret = RobotEngine2.SocketConnect();  }
      catch (Exception e)
      {  MessageBox.Show("WIFI初始化失败:" + e.Message, "WIFI初始化失败提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      return ret;
      }
    我已经实例化了RobotEngine2.SOCKET和RobotEngine2.IPE了,为什么调试的时候我发现socket无法赋值给RobotEngine2.SOCKET,ipe无法赋值给RobotEngine2.IPE?而且RobotEngine2.SOCKET和RobotEngine2.IPE都为null,这是什么问题?
    我把能实例化的都弄了,为什么还是空?