tModLoader Issues with Main.tile[x, y] returning null

Ahndrek Li'Cyri

Skeletron Prime
Hello!
I'm not sure if this is a bug or just shoddy coding on my part, but every once in awhile when using Main.tile[x, y] during world generation, it randomly returns null!
There doesn't seem to be any pattern nor any reason for it to do so, and when it happens the game totally locks up because I get a error.
It is to my knowedge that Main.tile[x, y].active() should return false if there is no tile there, so I'm very confused why I'm getting just completely null tiles at all.
Any help is appreciated, thank you!

Caller function:
C#:
progress.Message = "Burying Hidden Tech...";
            for (int k = 0; k < (int)((Main.maxTilesX * Main.maxTilesY) * 6E-05); k++)
            {
                int x = WorldGen.genRand.Next(0, Main.maxTilesX);
                int y = WorldGen.genRand.Next((int)WorldGen.rockLayer, Main.maxTilesY);

                // This seems to do nothing to prevent the crash, which is odd...
                if(Main.tile[x, y].active())
                {
                    mod.Logger.Info("X: " + x +", Y: " + y + ", isActive: " + Main.tile[x, y].active());
                    GenerateOval(x, y, Ore.OldTech_Radius_Width, Ore.OldTech_Radius_Height, (ushort)TileType<Tiles.CompactOldTech_Ore>(), false);
                }
            }
Here is the custom function I use:
C#:
private void GenerateOval(int cX, int cY, int rX, int rY, ushort TileID, bool fillEmpty = false)
        {
            for (double angle = 0; angle <= 1 * Math.PI; angle += 0.001)
            {
                Vector2 PosTile = new Vector2(cX + (int)(rX * Math.Cos(angle)), cY + (int)(rY * Math.Sin(angle)));
                Vector2 NegTile = new Vector2(cX - (int)(rX * Math.Cos(angle)), cY - (int)(rY * Math.Sin(angle)));
                for (int i = (int)NegTile.X; i < (int)PosTile.X; i++)
                {
                    for (int j = (int)NegTile.Y; j < (int)PosTile.Y; j++)
                    {
                        if(Main.tile[i, j] == null)
                        {
                            mod.Logger.Error("Missing tile! X(i):" + i + ", Y(j):" + j);
                            return;
                        }

                        if (Main.tile[i, j].active()) // Error usually happens here if the above IF statement was not there
                        {
                            Main.tile[i, j].type = TileID;
                            Main.tile[i, j].slope(0);
                        }
                        else
                        {
                            if (fillEmpty)
                            {
                                WorldGen.PlaceTile(i, j, TileID);
                            }
                        }
                    }
                }
            }
        }
Here is the generated error when i don't specifically check for if a tile is null:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=TechAndDragons
StackTrace:
at TechAndDragons.TAD_World.GenerateOval(Int32 cX, Int32 cY, Int32 rX, Int32 rY, UInt16 TileID, Boolean fillEmpty) in C:\Users\[Username]\Documents\My Games\Terraria\ModLoader\Mod Sources\TechAndDragons\TAD_World.cs:line 78
 
Top Bottom