Hi, coding a IRC Bot I have came across this when I tried run in debugging.


NullReferenceException was unhandled @ this code

// Send PING to irc server every 15 seconds
    public void Run()
    {
        while (true)
        {
            IrcBot.writer.WriteLine(PING + IrcBot.SERVER);
            IrcBot.writer.Flush();
            Thread.Sleep(15000);
        }
    }
}

Complete code of files:

IrcBot.cs

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
/*
* This program establishes a connection to irc server, joins a channel and greets every nickname that
* joins the channel.
*
* Coded by Pasi Havia 17.11.2001 http://koti.mbnet.fi/~curupted
*/
class IrcBot
{
    public static string SERVER = "irc.swiftirc.net";
   
      // StreamWriter is declared here so that PingSender can access it
    public static StreamWriter writer;
    public static void Head(string[] args)
    {
        NetworkStream stream;
        TcpClient irc;
        string inputLine;
        StreamReader reader;
       
    }
}

PingSender.cs:

using System;
using System.Threading;
/*
* Class that sends PING to irc server every 15 seconds
*/
class PingSender
{
    static string PING = "PING :";
    private Thread pingSender;
    // Empty constructor makes instance of Thread
    public PingSender()
    {
        pingSender = new Thread(new ThreadStart(this.Run));
    }
    // Starts the thread
    public void Start()
    {
        pingSender.Start();
    }
    // Send PING to irc server every 15 seconds
    public void Run()
    {
        while (true)
        {
            IrcBot.writer.WriteLine(PING + IrcBot.SERVER);
            IrcBot.writer.Flush();
            Thread.Sleep(15000);
        }
    }
}

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Irc server to connect
        
        // Irc server's port (6667 is default port)
        private static int PORT = 6667;
        // User information defined in RFC 2812 (Internet Relay Chat: Client Protocol) is sent to irc server
        private static string USER = "USER Onzichtbaar 8 * :by _joel";
        // Bot's nickname
        private static string NICK = "blah";
        //
        private static string NSPASS = "blah";
        // Channel to join
        private static string CHANNEL = "#ur-dead324";
        public static StreamWriter writer;
       

        private void button1_Click(object sender, EventArgs e)
        {
            NetworkStream stream;
            TcpClient irc;
            string inputLine;
            StreamReader reader;
            try
            {
                irc = new TcpClient(IrcBot.SERVER, PORT);
                stream = irc.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(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();
                writer.WriteLine("PRIVMSG " + "NickServ " + "id " + NSPASS);
                writer.Flush();
                Thread.Sleep(2000);
                writer.WriteLine("PRIVMSG " + CHANNEL + " " + ".login");
                while (true)
                {
                    while ((inputLine = reader.ReadLine()) != null)
                    {
                        if (inputLine.EndsWith(".scash"))
                        {
                            writer.WriteLine("PRIVMSG " + CHANNEL + " " + "Onzichtbaar cash script by _joel started.");
                            int length = 101;
                            for (int i = 0; i < length; i++)
                            {
                                writer.WriteLine("PRIVMSG " + CHANNEL + " " + ".cash");
                                writer.WriteLine("PRIVMSG " + CHANNEL + " " + ".lot");
                                Thread.Sleep(1467962);
                            }

                        }
                    }



                    // Close all streams
                    writer.Close();
                    reader.Close();
                    irc.Close();
                }
            }
            catch (Exception)
            {
                // Show the exception, sleep for a while and try to establish a new connection to irc server
                Console.WriteLine(e.ToString());
                Thread.Sleep(5000);
                string[] argv = { };
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public Form1()
        {
            InitializeComponent();
        }
    }

}

where IrcBot.writer will be assigned?

You aren't initialising an instance of the IrcBot class. You need to either make the class static if you plan to use a single instance of it or you need to create an instance to use inside your Run method:

IrcBot botInstance = new IrcBot();
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.