Could someone tell me what I’m doing wrong with this code?
My visual studio is saying this line of code is causing the issue public void joinRoom(string channel)
{
outputStream.WriteLine(“JOIN #” + channel);
outputStream.Flush();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
What’s the error/exception Visual Studio gives you? Are you sure the connection is still alive at that point?
Also, clients don’t need to send the hostmask, just PRIVMSG #channel :message is enough, and you’re missing a space between PRIVMSG and # in sendChatMessage.
This would suggest that outputStream is null, which usually means it was never initialized in the first place.
You seem to have two constructors that does two different things. One that initializes outputStream and one that does not. Every constructor should initialize every member variable. Multiple constructors are usually set up so that one takes the most possible parameters and the others takes less by explicitly calling the first constructor with hard-coded default values.
You cannot divide initialization over several constructors. If you feel that the parameter list would be too long to initialize all variables you can wrap related parameters in other classes or containers
Besides my above point being a fault in your code I see the real problem now. You have a private string member variable called channel which is never initialized (and does not look like it is intended to be used). The joinRoom method has a parameter with the same name, and the member variable takes precedence in the confusion. So the line outputStream.WriteLine("JOIN #" + channel); is actually called on the uninitialized member variable instead of the parameter with the same name.
Change the line to: this.channel = channel; outputStream.WriteLine("JOIN #" + this.channel);
or better yet, adopt a better naming convention to avoid future mixups.
The problem isn’t the channel parameter in the method with the same name as a parameter of the class, it’s still using the one of the method unless you call it up using the this prefix. It’s still a good idea to get a better naming convention. ReSharper will warn for issues like this on the fly.
I think something goes wrong in the actual code calling the method, or even better at the constructor.
Can you show use the line of code where you are constructing the IrcClient?
If the login fails, the writer stream will be null.
The easiest way to debug this yourself is to use visual studio’s code stepper, set a break point on the first entry point to your code, and use step into until you reach the exception. You can also use try{}catch(Exception ex) {Console.WriteLine(ex.ToString())} to find the method that is causing the issue.
My guess is that your password is incorrect, and the login is failing, you aren’t handling the error and instead try to get the stream for the reader/writer, and that’s failing and throwing the error. Your GetStream object shouldn’t be null, and the reader should immediately return the new server message with GL HF or w/e it is.