C#C
C#2y ago
Ethan

Object reference not set to an instance of an object

I'm making a simple UDP packet sender but for some reason I have this error when I attempt to start sending. which is "Object reference not set to an instance of an object. here is my code.
    public partial class Form1 : Form
    {
        private static string hexStream;
        private static UdpClient udpClient;
        private static byte[] packetData = { 0x01, 0x02 };

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string ipAddress = textBox1.Text;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false;
            }

            if (checkBox6.Checked == true && checkBox2.Checked == true)
            {
                checkBox2.ForeColor = Color.Green;
            }
            else
            {
                checkBox2.ForeColor = Color.Black;
            }

            if (checkBox2.ForeColor == Color.Green)
            {
                int port = 80;
                UdpClient udpClient = new UdpClient();
                string ipAddress = textBox1.Text;

                Console.WriteLine();
                hexStream = Console.ReadLine();

                Console.WriteLine();
                int rate = int.Parse(Console.ReadLine());

                int interval = 1 / rate;

                udpClient.Send(packetData, packetData.Length, ipAddress, port);
            }
            else
            {
                udpClient.Close();
            }
        }
Was this page helpful?