Creating a custom class with interface returns "Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object"

Created at 26 Feb 2023, 14:49
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
LA

lagostada

Joined 20.02.2023

Creating a custom class with interface returns "Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object"
26 Feb 2023, 14:49


Dear,

I'm trying to add a custom class that also uses the cAlgo.API classes, but Visual Studio 2022 is returing the cAlgo.API classes are not valid in the custom.

If I add the bot name as an interface, it will understand the cAlgo.API, but it will return an error on cTrader desktop with a crash.

Can you aid?


namespace cAlgo.Robots. 
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot2 : Robot
    {

    public string sn = "US500";
        protected override void OnStart()
        {

            PositionStatus ini = new PositionStatus(); //creates an instance of PositionStatus class
            MessageBox.Show(ini.ListIdPrice()[0].ToString()); //tries to display the first position of a list of prices

    }
     }

    public class PositionStatus : NewcBot2 //I added the interface NewcBot2 here, but the method ListIdPrice will crash on ctrader desktop with NullReferenceException
    {

        public List<KeyValuePair<double, double>> ListIdPrice()
        {
            Dictionary<double, double> positionsdictionary = new Dictionary<double, double>();
            foreach (var position in Positions) //Without the interface NewcBot2 above, VisualStudio 2022 will indicate "Positions" here is a 'type' which is not valid in the current context. Positions is a class from cAlgo.API.Positions
            {
                if (position.SymbolName == sn)
                {
                    positionsdictionary.Add(position.Id, position.EntryPrice);
                }

            }
            List<KeyValuePair<double, double>> list = positionsdictionary.ToList();
            return list;

        }
    }
}


@lagostada