NullReferenceException in coroutine when using a function

Anyone know what I’m doing wrong with this coroutine ?

This works fine :

 StartCoroutine( cm.GetChart(strFlag) );

but this call that calls the same function, but in the class gives back a NullReferenceException

 cm.CountryChange(strFlag);

This is in cm Class

public void CountryChange(string strFlag)
{
	StartCoroutine( GetChart(strFlag) );
}	

public IEnumerator GetChart(string strCountry)
{
......
}

Your problem is that you are trying to create a MonoBehaviour using “new”. This is expressly forbidden.

A monobehaviour MUST be owned by a GameObject to make sense.

You need to either add it in the inspector and use

GetComponent<ChartManager>()

OR use

gameObject.AddComponent<ChartManager>()

Either way do NOT use “new”

What’s cm? Is that being instantiated before?, I believe that the null reference should be because the method is not static and it requires an instance of a class. If the call and the method are within the same class… you just need to call CountryChange(strFlag) without the “cm.” before

Yes! Thank you. The GameObject (Context) on which the CoRoutine is running must still EXIST!
I added a DontDestroyOnLoad to the GameObject to solve this. (I actually was actually using the CoRoutine on a Singleton)