Null Reference Exception query

Created at 29 Jul 2014, 17:40
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!
AP

apresau

Joined 31.03.2014

Null Reference Exception query
29 Jul 2014, 17:40


Hi,

I'm having trouble with a crashing cBot.

The log message reads -

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

The offending code is associated with the array -

        private double[] ExitPrice = new double[20];


        protected override void OnTick()
        {

// trade entry ---------
<<open trade position>>
               SetExitPriceArray();


// trade exit ---------
            if (Positions.Count > 0)
            {
                TradeCnt = Positions.Count;

                if (position.TradeType == TradeType.Buy)
                {
                    if (Ask <= ExitPrice[TradeCnt - 1])
                    {
                        CloseAllTrades();
                        return;
                    }
                }

                if (position.TradeType == TradeType.Sell)
                {
                    if (Bid >= ExitPrice[TradeCnt - 1])
                    {
                        CloseAllTrades();
                        return;
                    }
                }
            }
        }



// FUNCTIONS ------------------------------------------------------------------------------

<< other functions>>

        private void SetExitPriceArray()
        {
            double RefPrice;
            if (position.TradeType == TradeType.Buy)
            {
                RefPrice = Symbol.Ask;

                for (int i = 1; i < 4; i++)
                {
                    ExitPrice[i] = RefPrice;
                }
                for (int i = 4; i < 20; i++)
                {
                    ExitPrice[i] = RefPrice + (PipStep * Symbol.TickSize) * (i - 3);
                }
            }


            if (position.TradeType == TradeType.Sell)
            {
                RefPrice = Symbol.Bid;

                for (int i = 1; i < 4; i++)
                {
                    ExitPrice[i] = RefPrice;
                }
                for (int i = 4; i < 20; i++)
                {
                    ExitPrice[i] = RefPrice + (PipStep * Symbol.TickSize) * (i - 3);
                }
            }
        }
    }
}

I don't understand enough to be able to interpret the log entry.

If someone could help I'd be most grateful.

apresau


@apresau
Replies

Invalid
29 Jul 2014, 17:52

RE:

Could you provide full code? 

Most probably position variable is not initialized.

apresau said:

Hi,

I'm having trouble with a crashing cBot.

The log message reads -

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

The offending code is associated with the array -

        private double[] ExitPrice = new double[20];


        protected override void OnTick()
        {

// trade entry ---------
<>
               SetExitPriceArray();


// trade exit ---------
            if (Positions.Count > 0)
            {
                TradeCnt = Positions.Count;

                if (position.TradeType == TradeType.Buy)
                {
                    if (Ask <= ExitPrice[TradeCnt - 1])
                    {
                        CloseAllTrades();
                        return;
                    }
                }

                if (position.TradeType == TradeType.Sell)
                {
                    if (Bid >= ExitPrice[TradeCnt - 1])
                    {
                        CloseAllTrades();
                        return;
                    }
                }
            }
        }



// FUNCTIONS ------------------------------------------------------------------------------

<< other functions>>

        private void SetExitPriceArray()
        {
            double RefPrice;
            if (position.TradeType == TradeType.Buy)
            {
                RefPrice = Symbol.Ask;

                for (int i = 1; i < 4; i++)
                {
                    ExitPrice[i] = RefPrice;
                }
                for (int i = 4; i < 20; i++)
                {
                    ExitPrice[i] = RefPrice + (PipStep * Symbol.TickSize) * (i - 3);
                }
            }


            if (position.TradeType == TradeType.Sell)
            {
                RefPrice = Symbol.Bid;

                for (int i = 1; i < 4; i++)
                {
                    ExitPrice[i] = RefPrice;
                }
                for (int i = 4; i < 20; i++)
                {
                    ExitPrice[i] = RefPrice + (PipStep * Symbol.TickSize) * (i - 3);
                }
            }
        }
    }
}

I don't understand enough to be able to interpret the log entry.

If someone could help I'd be most grateful.

apresau

 


@Invalid

Spotware
29 Jul 2014, 17:53

RE:

You can debug your code using Visual Studio. It will help you to get full details about your exception. 

apresau said:

Hi,

I'm having trouble with a crashing cBot.

The log message reads -

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.


@Spotware

apresau
30 Jul 2014, 05:22

RE: RE:

Thanks for your fast response, guys. Much appreciated.

In trying to track the problem I commented out all lines and functions referencing the RefPrice array.

The program ran fine. Which makes me think that my array syntax is wanting.

Looks like I'll have to download Visual Studio. (been avoiding it due to our awful internet speeds)

Invalid said:

Could you provide full code? 

Most probably position variable is not initialized.

apresau said:

Hi,

I'm having trouble with a crashing cBot.

The log message reads -

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

The offending code is associated with the array -

        private double[] ExitPrice = new double[20];


        protected override void OnTick()
        {

// trade entry ---------
<>
               SetExitPriceArray();


// trade exit ---------
            if (Positions.Count > 0)
            {
                TradeCnt = Positions.Count;

                if (position.TradeType == TradeType.Buy)
                {
                    if (Ask <= ExitPrice[TradeCnt - 1])
                    {
                        CloseAllTrades();
                        return;
                    }
                }

                if (position.TradeType == TradeType.Sell)
                {
                    if (Bid >= ExitPrice[TradeCnt - 1])
                    {
                        CloseAllTrades();
                        return;
                    }
                }
            }
        }



// FUNCTIONS ------------------------------------------------------------------------------

<< other functions>>

        private void SetExitPriceArray()
        {
            double RefPrice;
            if (position.TradeType == TradeType.Buy)
            {
                RefPrice = Symbol.Ask;

                for (int i = 1; i < 4; i++)
                {
                    ExitPrice[i] = RefPrice;
                }
                for (int i = 4; i < 20; i++)
                {
                    ExitPrice[i] = RefPrice + (PipStep * Symbol.TickSize) * (i - 3);
                }
            }


            if (position.TradeType == TradeType.Sell)
            {
                RefPrice = Symbol.Bid;

                for (int i = 1; i < 4; i++)
                {
                    ExitPrice[i] = RefPrice;
                }
                for (int i = 4; i < 20; i++)
                {
                    ExitPrice[i] = RefPrice + (PipStep * Symbol.TickSize) * (i - 3);
                }
            }
        }
    }
}

I don't understand enough to be able to interpret the log entry.

If someone could help I'd be most grateful.

apresau

 

 


@apresau