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