-
Jul 1st, 2005, 03:15 PM
#1
Thread Starter
Frenzied Member
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;
}
}
}
}
-
Jul 1st, 2005, 07:07 PM
#2
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 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
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 1st, 2005, 09:57 PM
#3
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.
-
Jul 2nd, 2005, 02:57 PM
#4
Re: Object Refference is Not Set to an Instance of an Object
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 2nd, 2005, 10:44 PM
#5
Thread Starter
Frenzied Member
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
-
Jul 3rd, 2005, 05:15 AM
#6
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.
-
Jul 3rd, 2005, 11:05 AM
#7
Frenzied Member
Re: Object Refference is Not Set to an Instance of an Object
In C# arrays start from 0. So 0 to 299 is 300
-
Jul 4th, 2005, 11:52 AM
#8
Thread Starter
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|