Results 1 to 8 of 8

Thread: Object Refference is Not Set to an Instance of an Object

  1. #1

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228

    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;
    	        }
            }
        }
    }
    Luke

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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!!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Object Refference is Not Set to an Instance of an Object

    exactly
    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!!

  5. #5

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228

    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
    Luke

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  7. #7
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    Re: Object Refference is Not Set to an Instance of an Object

    In C# arrays start from 0. So 0 to 299 is 300

  8. #8

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228

    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.
    Luke

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width