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();
}
}
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();
}
}
65 replies
✅ Help with windows forms check uncheck thing
I want it to be where only 1 checkbox can be checked at a time, I tried this but it is not working, any help?
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if(checkBox2.Checked && (checkBox3.Checked || checkBox4.Checked || checkBox5.Checked))
{
checkBox2.Checked = false;
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if(checkBox2.Checked && (checkBox3.Checked || checkBox4.Checked || checkBox5.Checked))
{
checkBox2.Checked = false;
}
}
9 replies