Q:

Null reference in a foreach over "Table.Rows"

In the line foreach (DataRow Row in Table.Rows), I'm getting a NullReferenceException.
The program is in an emulator in C#.  When I debug it and it crashes, it highlights the word foreach in yellow.  I've pasted my whole code below.
using System;
using System.Collections.Generic;
using System.Data;

using Reality.Storage;
using System.Threading;

namespace Reality.Game.Moderation
{
    public static class ModerationBanManager
    {
        private static List<uint> mCharacterBlacklist;
        private static List<string> mRemoteAddressBlacklist;
        private static Thread mWorkerThread;
        private static object mSyncRoot;

        public static void Initialize(SqlDatabaseClient MySqlClient)
        {
            mCharacterBlacklist = new List<uint>();
            mRemoteAddressBlacklist = new List<string>();
            mSyncRoot = new object();

            mWorkerThread = new Thread(new ThreadStart(ProcessThread));
            mWorkerThread.Name = "ModerationBanManager";
            mWorkerThread.Priority = ThreadPriority.Lowest;
            mWorkerThread.Start();

            ReloadCache(MySqlClient);
        }

        public static void ProcessThread()
        {
            try
            {
                while (Program.Alive)
                {
                    Thread.Sleep(600000);

                    using (SqlDatabaseClient MySqlClient = SqlDatabaseManager.GetClient())
                    {
                        ReloadCache(MySqlClient);
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (ThreadInterruptedException) { }
        }

        public static void ReloadCache(SqlDatabaseClient MySqlClient)
        {
            lock (mSyncRoot)
            {
                mCharacterBlacklist.Clear();
                mRemoteAddressBlacklist.Clear();

                MySqlClient.SetParameter("timestamp", UnixTimestamp.GetCurrent());
                DataTable Table = MySqlClient.ExecuteQueryTable("SELECT * FROM bans WHERE timestamp_expire > @timestamp");

                foreach (DataRow Row in Table.Rows)
                {
                    uint UserId = (uint)Row["user_id"];
                    string RemoteAddr = (string)Row["remote_address"];

                    if (UserId > 0 && !mCharacterBlacklist.Contains(UserId))
                    {
                        mCharacterBlacklist.Add(UserId);
                    }

                    if (RemoteAddr.Length > 0 && !mRemoteAddressBlacklist.Contains(RemoteAddr))
                    {
                        mRemoteAddressBlacklist.Add(RemoteAddr);
                    }
                }
            }
        }

        public static bool IsRemoteAddressBlacklisted(string RemoteAddressString)
        {
            lock (mSyncRoot)
            {
                return mRemoteAddressBlacklist.Contains(RemoteAddressString);
            }
        }

        public static bool IsUserIdBlacklisted(uint UserId)
        {
            lock (mSyncRoot)
            {
                return mCharacterBlacklist.Contains(UserId);
            }
        }

        public static void BanUser(SqlDatabaseClient MySqlClient, uint UserId, string MessageText, uint ModeratorId)
        {
            MySqlClient.SetParameter("userid", UserId);
            MySqlClient.SetParameter("reason", MessageText);
            MySqlClient.SetParameter("timestamp", UnixTimestamp.GetCurrent());
            MySqlClient.SetParameter("timestampex", UnixTimestamp.GetCurrent());
            MySqlClient.SetParameter("moderator", ModeratorId);
            MySqlClient.ExecuteNonQuery("INSERT INTO bans (user_id,reason_text,timestamp_created,timestamp_expire,moderator_id) VALUES (@userid,@reason,@timestamp,@timestampex,@moderator)");

            lock (mSyncRoot)
            {
                mCharacterBlacklist.Add(UserId);
            }
        }
    }
}

A:

If it's really being thrown at this line:
foreach (DataRow Row in Table.Rows) 

Then Table or Table.Rows is null.  If it is being thrown from inside of your foreach loop:
foreach (DataRow Row in Table.Rows) 
{
   //aka in here
}

Then it most likely means that one of your rows is null.  To find which one add this at the beginning of your loop and place a break point inside of the if statement.
foreach (DataRow Row in Table.Rows) 
{
   if(Row == null)
   {
       //breakpoint here!
   }

}

You can also just examine Table.Rows while debugging, but if there are 1000s of rows this is not a realistic option.

A:

If you have NullReferenceException in that exact line, this means that either the Table variable or the Rows property is null. There are almost no other options. Please put a breakpoint there and check which one of them is null - and then investigate and correct that.

