C
C#14mo 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();
}
}
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();
}
}
43 Replies
Ethan
EthanOP14mo ago
I am pretty sure it has to do with the "string ip Address = textBox1.Text" but I am relatively new and do not know how to fix it
DeliBey
DeliBey14mo ago
private static UdpClient udpClient;
private static UdpClient udpClient;
You declared but didn't assign. use "new()" keyword.
Buddy
Buddy14mo ago
Next time, please show where the error occurs, you did not specify where the breakpoint hits / exception throws.
DeliBey
DeliBey14mo ago
Also Why not radioBox ? It would fit your application. Don't bother with checkBox values Also check for constructor(s) for UdpClient
Ethan
EthanOP14mo ago
I dont know, I like checkboxes, do they raise any problems? k yeah its happening because of the chekcbox Where do I put that?
ACiDCA7
ACiDCA714mo ago
this code is quite messy string ipAddress = textBox1.Text; in textBox1_TextChanged doesnt do anything really UdpClient udpClient = new UdpClient(); in if (checkBox2.ForeColor == Color.Green) is only available in that if clause not on the else where you are trying to close the client and what default wrote
DeliBey
DeliBey14mo ago
No description
Ethan
EthanOP14mo ago
hmm, so how would I make it send when its green and not send when its not? Also I want to keep it secluded to like that checkbox if that makes sense
DeliBey
DeliBey14mo ago
Check for constructors
Ethan
EthanOP14mo ago
like this?
Ethan
EthanOP14mo ago
No description
DeliBey
DeliBey14mo ago
Yeah you should see from there
Ethan
EthanOP14mo ago
ah how
DeliBey
DeliBey14mo ago
No description
DeliBey
DeliBey14mo ago
One of them accepts IPEndPoint right ? So you can pass an ipEndPoint object
DeliBey
DeliBey14mo ago
No description
DeliBey
DeliBey14mo ago
Like this
Buddy
Buddy14mo ago
I personally hate the wrappers, both Tcp / UDP wrappers are useless Raw sockets are way better
DeliBey
DeliBey14mo ago
Yeah but it's easier to use/learn 😛
Ethan
EthanOP14mo ago
I have muy errors
Ethan
EthanOP14mo ago
No description
DeliBey
DeliBey14mo ago
No description
DeliBey
DeliBey14mo ago
Your C# version is older so you need to specify the type when using "new" keyword
Ethan
EthanOP14mo ago
sweet, that fixed it. however
Ethan
EthanOP14mo ago
No description
DeliBey
DeliBey14mo ago
Just read it man 😦 They are indicated with red underline
Ethan
EthanOP14mo ago
ok I read but do not understand
DeliBey
DeliBey14mo ago
is a class not an object
No description
DeliBey
DeliBey14mo ago
It expects an object
DeliBey
DeliBey14mo ago
No description
DeliBey
DeliBey14mo ago
this is an object That you created with "new" keyword.
Ethan
EthanOP14mo ago
that makes sense thank you
DeliBey
DeliBey14mo ago
np 👍
Ethan
EthanOP14mo ago
sorry but 1 more thing
DeliBey
DeliBey14mo ago
Just ask
Ethan
EthanOP14mo ago
No description
DeliBey
DeliBey14mo ago
No description
DeliBey
DeliBey14mo ago
It expects either just endPoint or hostname with port as you see But you are passing ipEndPoint object which is not a string
DeliBey
DeliBey14mo ago
No description
DeliBey
DeliBey14mo ago
Hover the mouse on object and you can see it's type Which is "IPEndPoint?"
SinFluxx
SinFluxx14mo ago
They're presumably using the overload listed below that
DeliBey
DeliBey14mo ago
Yeah I'm just explaining the fundamentals of IDE and logic behind objects You can pass ipEndPoint without port
var ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80);

using (var udpClient = new UdpClient(ipEndPoint))
{
var messageBytes = Encoding.UTF8.GetBytes("Hello World!");

var bytesSent = udpClient.Send(messageBytes, messageBytes.Length, ipEndPoint);

udpClient.Send(messageBytes, messageBytes.Length, ipEndPoint);

Console.WriteLine($"Sent {bytesSent} bytes.");
}
var ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80);

using (var udpClient = new UdpClient(ipEndPoint))
{
var messageBytes = Encoding.UTF8.GetBytes("Hello World!");

var bytesSent = udpClient.Send(messageBytes, messageBytes.Length, ipEndPoint);

udpClient.Send(messageBytes, messageBytes.Length, ipEndPoint);

Console.WriteLine($"Sent {bytesSent} bytes.");
}
Ethan
EthanOP14mo ago
oh ok yeah that makes some more sense now thank you for help

Did you find this page helpful?