Hi everyone, I'm trying to connect to an IRC server and join a channel. When a user joins they will be greeted by my program. I'm not able to debug it because I'm getting an error saying 'Object reference not set to an instance of an object.' Here's my code:

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Text;

namespace IRCBot
{
    class IrcBot
    {
        //IRC server to connect to
        public static string SERVER = "irc.gamesurge.net";
        //irc servers port
        private static int PORT = 6667;
        //user information sent to IRC server
        private static string USER = "USER CSharpBot 8 * :I'm a C# IRC Bot.";
        //bots nickname
        private static string NICK = "nyquilBOT";
        //channel to join
        private static string CHANNEL = "#blah123";
        //streamwriter declared so pingsender can access it
        public static StreamWriter writer;
    
        static void Main(string[] args)
        {
            NetworkStream stream;
            TcpClient irc;
            string inputLine;
            StreamReader reader;
            string nickName;

            try
            {
                irc = new TcpClient(IrcBot.SERVER, IrcBot.PORT);
                stream = irc.GetStream();
                reader = new StreamReader(stream);
                //start pingsender thread
                PingSender ping = new PingSender();
                ping.Start();
                writer.WriteLine(USER);
                writer.Flush();
                writer.WriteLine("NICK " + NICK);
                writer.Flush();
                writer.WriteLine("JOIN " + CHANNEL);
                writer.Flush();

                while(true)
                {
                    while((inputLine = reader.ReadLine()) != null)
                    {
                        if(inputLine.EndsWith("JOIN :" + CHANNEL))
                        {
                            //parse nickname of person who joined the channel
                            nickName = inputLine.Substring(1, inputLine.IndexOf("!") - 1);
                            //welcome the nickname to the channel by sending a notice
                            writer.WriteLine("NOTICE " + nickName + " :Hi" + nickName + " and welcome to " + CHANNEL + "!");
                            writer.Flush();
                            //sleep to prevent excess flood
                            Thread.Sleep(2000);
                        }
                    }
                    //close all streams
                    writer.Close();
                    reader.Close();
                    irc.Close();
                }
            }
            catch (Exception e)
            {
                //close the connection, sleep for a while and try to establish a new connection
                Console.WriteLine(e.ToString());
                Thread.Sleep(5000);
                string[] argv = {};
                Main(argv);
            }

           
        }
    }

    //class that sends ping to irc server every 15 seconds
    class PingSender
    {
        private static string PING = "PING :";
        private Thread pingSender;
        //empty contructor makes instance of thread
        public PingSender()
        {
            pingSender = new Thread(new ThreadStart(this.Run));
        }

        public void Start()
        {
            pingSender.Start();
        }

        public void Run()
        {
            while (true)
            {
                IrcBot.writer.WriteLine(PING + IrcBot.SERVER);
                IrcBot.writer.Flush();
                Thread.Sleep(15000);
            }
        }

    }
}

I'm getting that error here under the Run() function that's inside of the PingSender class:

IrcBot.writer.WriteLine(PING + IrcBot.SERVER);

Recommended Answers

All 2 Replies

You have never created an instance of StreamWriter. Your declaration is

public static StreamWriter writer;

In the IrcBot class, but you've never at any point said

writer = new StreamWriter(somepath);

Or otherwise obtained a StreamWriter instance. Fix that error, give it another try, and report back if you encounter more bugs.

Ah of course. I've made an instance of streamwriter now. However, when I debug I get the following error: 'Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.' This error is occuring in this block of code from above:

#
writer.WriteLine(USER);
#
writer.Flush();
#
writer.WriteLine("NICK " + NICK);
#
writer.Flush();
#
writer.WriteLine("JOIN " + CHANNEL);
#
writer.Flush();
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.