TileMap NullReferenceException when calling SetTile

I am trying to create a terrain generator that works based on TileMaps but when I try to create a TileMap at runtime it gives me a NullReferenceException when calling the SetTile method. Here is my code. My Unity version is 2019.2.17f1 but I also tested on 2018.4.14f1 with the same result.

public static Tilemap GenerateTerrain(Planet planet)
    {
        Tilemap map = new Tilemap();

        for (int x = 0; x < planet.size; x++)
        {
            float height = Mathf.PerlinNoise(x / 60f, 0);

            int intHeight = Mathf.FloorToInt(height);

            Vector3Int tilePos = new Vector3Int(x, intHeight, 0);

            map.SetTile(tilePos, Tiles.grass); //This line gives me the error
        }

        return map;
    }

You need to have the Tile variable or some reference from the outside in the script

public Tile grass;
Vector3Int tilePos = new Vector3Int(x, intHeight, 0);
    
    map.SetTile(tilePos, grass);

PD: Dont forget to fill the Tile slot of the script with your tile in the editor

Solved it. Turns out that you cannot create a component without attaching it to a GameObject.