当前位置:编程学习 > C#/ASP.NET >>

求高手帮我看下这个错误该怎么改

在下面红字部分显示的错误是:未处理 System.NullReferenceException
  Message=未将对象引用设置到对象的实例。
  Source=掷易做图客户端
  StackTrace:
       在 掷易做图客户端.Form1.ListenMsg() 位置 E:\以前\掷易做图31\掷易做图31\掷易做图客户端\Form1.cs:行号 63
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//add
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace 掷易做图客户端
{
    public delegate void NewForm(string ip);
    public partial class Form1 : Form
    {
        Communication communicate=new Communication();
        ProgressBar pro = new ProgressBar();//状态条
        /// <summary>
        /// 判断是否连接上
        /// </summary>
        bool connected;
        public Form1()
        {
            InitializeComponent();
        }
        #region 进行连接
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "") 
            {
                MessageBox.Show("昵称不能为空");
                return;
            } 
            //实现进度条         
            pro.Location = new Point(42,161);
            pro.Size = new Size(181,27);
            pro.Style = ProgressBarStyle.Marquee;
            this.Controls.Add(pro);
            //进行连接
            if(!this.textBox2.Visible)
            communicate.SendMsg("join|"+this.textBox1.Text+"|"+communicate.MyIp,"192.168.255.255",1500);
            else if (CheckIP())
            {
                communicate.SendMsg("join|" + this.textBox1.Text + "|" + communicate.MyIp, this.textBox2.Text, 1500);
                pro.Visible = true;
            }
            else
            {
                MessageBox.Show("IP格式不正确");
                return;
            }
            Thread fail = new Thread(new ThreadStart(ConnectedCheck));
            fail.Start();
        }
        #endregion
        #region 监听服务器信息
        private void ListenMsg()
        {
            ////本指定端口
            UdpClient udpclient = new UdpClient(3000);
            IPEndPoint remote=null ;
            MessageBox.Show(remote.Address.ToString()+"1");
            while (true)
            {

                try
                {

                    byte[] bytes = udpclient.Receive(ref remote);
                    MessageBox.Show(remote.Address.ToString() + "1");
                    string recieve = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    string[] message = recieve.Split('|');
                    if (message[0] == "connect")
                    {
                        if (communicate.MyIp != message[3]) continue;
                        if (message[2]== "1")
                        {
                            this.Invoke(new NewForm(ProductForm), message[1]);
                            udpclient.Close();
                            break;
                        }
                        else
                        {
                            connected = true;
                            MessageBox.Show("已经存在同样的名字");
                        }
                    }
                }
                catch
                {

                }
            }
        }
        #endregion
        #region 检查是否已经连接上
        private void ConnectedCheck()
        {
            Thread.Sleep(8000);
            if (!connected)
            {
                this.Invoke(new MethodInvoker(SerachFail));
            }
        }
        #endregion
        #region 未连接上时所采取的反应
        private void SerachFail()
        {
            DialogResult d;
            d = MessageBox.Show("本网络不存在服务器,是否需要跨网络搜索?", "猜易做图", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            if (d == DialogResult.Yes)
            {
                label4.Visible = true;
                this.textBox2.Visible = true;
                
            }
            pro.Visible = false;
                return;
        }
        #endregion
        #region 检查IP
        private bool CheckIP()
        {
            try
            {
                string[] bIP = this.textBox2.Text.Split('.');
                for (int i = 0; i < 4; i++)
                {
                    int a = Int32.Parse(bIP[i]);
                    if (a < 0 || a > 255)
                        return false;
                }
                return true;
            }
            catch 
            {
                return false;
            }
        }
        #endregion
        #region 创建子窗体
        private void ProductForm(string ip)
            {
                connected=true;
                this.Hide();
                HouseForm house=new HouseForm(ip,this.textBox1.Text);
                house.Show(this);
             
            }
        #endregion
        private void Form1_Load(object sender, EventArgs e)
            {
                Thread mythread = new Thread(new ThreadStart(ListenMsg));
                //将线程设为后台运行
                mythread.IsBackground = true;
                mythread.Start();
             
            }

        
        }
    }





--------------------编程问答-------------------- remote=null;

错误很明显没有实例化 --------------------编程问答--------------------
引用
remote=null;

错误很明显没有实例化

我刚学不久,这个应该怎么改呢?谢谢
--------------------编程问答--------------------
IPEndPoint remote = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80);//两个参数,一个为IP地址,一个为端口号
--------------------编程问答-------------------- 这一行抛出异常的时候,vs会进入调试器,你此时调试了吗?

如果调试了,你贴出来 remote.Address 是什么值。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,