C
C#4mo 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
Ethan4mo 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
Servator
Servator4mo ago
private static UdpClient udpClient;
private static UdpClient udpClient;
You declared but didn't assign. use "new()" keyword.
Buddy
Buddy4mo ago
Next time, please show where the error occurs, you did not specify where the breakpoint hits / exception throws.
Servator
Servator4mo ago
Also Why not radioBox ? It would fit your application. Don't bother with checkBox values Also check for constructor(s) for UdpClient
Ethan
Ethan4mo 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
ACiDCA74mo 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
Servator
Servator4mo ago
No description
Ethan
Ethan4mo 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
Servator
Servator4mo ago
Check for constructors
Ethan
Ethan4mo ago
like this?
Ethan
Ethan4mo ago
No description
Servator
Servator4mo ago
Yeah you should see from there
Ethan
Ethan4mo ago
ah how
Servator
Servator4mo ago
No description
Servator
Servator4mo ago
One of them accepts IPEndPoint right ? So you can pass an ipEndPoint object
Servator
Servator4mo ago
No description
Servator
Servator4mo ago
Like this
Buddy
Buddy4mo ago
I personally hate the wrappers, both Tcp / UDP wrappers are useless Raw sockets are way better
Servator
Servator4mo ago
Yeah but it's easier to use/learn 😛
Ethan
Ethan4mo ago
I have muy errors
Ethan
Ethan4mo ago
No description
Servator
Servator4mo ago
No description
Servator
Servator4mo ago
Your C# version is older so you need to specify the type when using "new" keyword
Ethan
Ethan4mo ago
sweet, that fixed it. however
Ethan
Ethan4mo ago
No description
Servator
Servator4mo ago
Just read it man 😦 They are indicated with red underline
Ethan
Ethan4mo ago
ok I read but do not understand
Servator
Servator4mo ago
is a class not an object
No description
Servator
Servator4mo ago
It expects an object
Servator
Servator4mo ago
No description
Servator
Servator4mo ago
this is an object That you created with "new" keyword.
Ethan
Ethan4mo ago
that makes sense thank you
Servator
Servator4mo ago
np 👍
Ethan
Ethan4mo ago
sorry but 1 more thing
Servator
Servator4mo ago
Just ask
Ethan
Ethan4mo ago
No description
Servator
Servator4mo ago
No description
Servator
Servator4mo ago
It expects either just endPoint or hostname with port as you see But you are passing ipEndPoint object which is not a string
Servator
Servator4mo ago
No description
Servator
Servator4mo ago
Hover the mouse on object and you can see it's type Which is "IPEndPoint?"
SinFluxx
SinFluxx4mo ago
They're presumably using the overload listed below that
Servator
Servator4mo 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
Ethan4mo ago
oh ok yeah that makes some more sense now thank you for help