Object Refference is Not Set to an Instance of an Object
I am new to C#, and I've tried what seems to be everything, and the error Object Refference is Not Set to an Instance of an Object. I'm using the Mono C# compiler and here is the complete code to my program thus far. Can anyone tell me what's wrong?
Code:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
// Program start class
public class Users {
public Users() {
public TcpClient client = new TcpClient();
public bool LoggedOn = false;
public NetworkStream stream = new NetworkStream();
}
}
class Listener {
// Main begins program execution.
public static void Main() {
Int32 port = 1337;
IPAddress localhost = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(localhost, port);
listener.Start();
bool program_run = true;
int i = 0;
int stream_len = 0;
Users[] user = new Users[300];
Byte[] bytes = new Byte[1024];
string dat=" ";
while (program_run) {
if (listener.Pending()) {
i=0;
while (!(user[i].LoggedOn)) {
i++;
}
user[i].LoggedOn=true;
user[i].client=listener.AcceptTcpClient();
user[i].stream=user[i].client.GetStream();
Console.WriteLine("Connected!");
}
i=0;
while (i<301) {
if (user[i].LoggedOn) {
if (user[i].stream.Length>0) {
bytes = new Byte[user[i].stream.Length];
stream_len = user[i].stream.Read(bytes,0,stream_len);
dat = Encoding.ASCII.GetBytes(bytes, 0, stream_len);
Console.WriteLine("Received Data: {0}", stream_len);
}
}
i=0;
}
}
}
}
Re: Object Refference is Not Set to an Instance of an Object
well that error means that you're trying to access a reference-type variable, while the value of that variable is set to null. Now one thing before anything, this is too weird:D In the constructor of Users class, you've declared variables with scopes. I dunno if it's a thing with mono, but microsoft's version of C# doesnt allow that. It just doesn't make any sense to be able to declare scope for a varible that's inside a function (because a variable inside a function is visible to the function alone, so declaring it as public, private, etc wouldn't make sense)......
also you have to set breakpoints and whatnot to figure out what line causes the error. By just looking at it, I would guess that the error is from this line?
while (!(user[i].LoggedOn)) {
before you've declared an array of type Users[], but you also need to initialize each array element. (ie, for each array element, you need to do user[i] = new Users())
hope it helps
Re: Object Refference is Not Set to an Instance of an Object
Exactly. You have declared an array that can store 300 Users objects but you haven't created any objects. It's like declaring 300 different variables of type Users and never assigning new Users objects to those variables.
Re: Object Refference is Not Set to an Instance of an Object
Re: Object Refference is Not Set to an Instance of an Object
Ah, I'm sorry! I'm very new to C#. I thought that when you declared an array of object variables it initializes them all.
Once again, I'm just learning the language.
Thanks for the help, it made me take some serious strides.
Luke
Re: Object Refference is Not Set to an Instance of an Object
I notice that your i counter is allowed to reach 300 even though your users[] array is only declared as 0 to 299.
Re: Object Refference is Not Set to an Instance of an Object
In C# arrays start from 0. So 0 to 299 is 300
Re: Object Refference is Not Set to an Instance of an Object
Yes, I learned all this. I asked this question on my first night. I was stumped, and now I feel like an idiot.