i have got an exception, i dont understand why? basically, below there is the bottom part of my server code that i try to use.

In short, i built 2 Dictionaries of streamWriter and StreamReader and one List of names.
Exception is thrown there:

Some of the collections that i use
    Dictionary<string, StreamWriter> dicWrite = new Dictionary<string, StreamWriter>();
    Dictionary<string, StreamReader> dicRead = new Dictionary<string, StreamReader>();
    Dictionary<string, string> pmChatters = new Dictionary<string, string>();
    StreamReader[] sr1 = new StreamReader[100];
    StreamWriter[] sw1 = new StreamWriter[100];
    List<string> names = new List<string>();

//The server thread function that reads and writes to the clients
 public void ProssecClient(object o)
    {
        TcpClient connection = o as TcpClient;
        if (connection == null)
        {
            return;
        }
       // here i try to obtain every stream and put it into an array of streams
// a and m are ints, they go up in number , each time a user logs in and receives a TCP reference..
        sr1[a++] = new StreamReader(connection.GetStream());
        sw1[m++] = new StreamWriter(connection.GetStream());
   

        int i = 0;
        try
        {
            while (true)
            {
                
        i = 0;
                inputObtained = sr.ReadLine();

            
    
                

                // Obtains a name (this function seperates what was sent with the name attached). dont worry about this. it works, it returns the name of the client
                currentSender = checkForName(inputObtained); 

                //Adds names that dont exist, in the server , (it regeistered a new user)
                if (!names.Contains(currentSender))
                {
                    names.Add(currentSender);
                    dicWrite.Add(names[i], sw1[m]);
          
                }

                
                 for ( i = 0; i <= m; i++)
			     {

                     if (names[i]!=null)
                     {
                         //If the Chatter is not in pm
                      if (pmChatters != null && !pmChatters.ContainsKey(names[i]))
                      {
                        dicWrite[names[i]].WriteLine(inputObtained);//The exception, is constatnly thrown here
                        dicWrite[names[i]].Flush();
                      }
                        //if the chatter is in pm
                      else if (names[i] != null && pmChatters != null && pmChatters.ContainsKey(names[i]))
                      {
                        dicWrite[pmChatters[currentSender]].WriteLine(inputObtained);
                        dicWrite[pmChatters[currentSender]].Flush();
                      }
                      else
                      {
                          break;
                      }
                   }
                 }

            }
        }
        catch {


            Console.WriteLine(currentSender + ". Name of user:" + names[0] + ". Current User: " + currentSender + ". " +
                    " m is"+m + " i is "+ i + " " + " client left");

            
        }
    }

Recommended Answers

All 7 Replies

m is always 1 more than the number of items you have (see line 20). In line 48 you are looping from 0 to m (your <=) so there is no element names when i == m, thus a null reference exception. Try changing line 48 to for (i = 0; i < m; i++) .

Also line 43 always adds names, and you set i to 0 before that, so you always get names[0] added.

i changed the last part of my code to 3, now both i and m are 1 , and int s=0.. it wont print 3 (wont go inside the loop)..take a look

for (int  s = 0;s <= n; s++)
			     {                   
                         //If the Chatter is not in pm
                     Console.WriteLine(2);
                      if (!pmChatters.ContainsKey(names[s]))//Exception is thrown saying Arguments out of range..
                      {
                          
                          sw = dicWrite[names[s]];//always null
                          Console.WriteLine(1);
                          if (sw != null)// sw is always null.
                          {
                                   Console.WriteLine(3);
                              sw.WriteLine(inputObtained);
                              sw.Flush();
                         
                             
                          }
                      }
                      else if (pmChatters.ContainsKey(names[s]))
                      {
                          Console.WriteLine(4);
                        dicWrite[pmChatters[currentSender]].WriteLine(inputObtained);
                        dicWrite[pmChatters[currentSender]].Flush();
                          
                      }
                      else
                      {Console.WriteLine(5);
                          break;
                      }
                   }
                

            }
        }

Edit. i found the mistake. i did this:

m-1. i think when a streamWriter was registered it was 0..but after that it became 1..therefore the stream that went to a dictionary array was empty.

i have another question , do you know how i can find a substring in a string. i want to find 2@3 in every string that is obtained by the ReadLine

i need to return a string, and not a number. does it return a string. i need a string to be returned..

My goal is to find that the symbol 2@3 is in
Dani2@3Moori

whats the best way to achieve this...

if a bool can be returned saying me that it is false or true, would be the most efficeint option..

return myString.IndexOf("2@3") >= 0 ? true : false;

a very nice algorithm. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.