How to use break?

FranK Duc 121 Reputation points
2021-03-01T18:56:52.413+00:00

Hello,

I am trying to figure out why i get this message error:

Error on calling 'OnRender' method on bar 513: Object reference not set to an instance of an object.

My chart has 513 bars in it. I loop throught all the bars of the chart and create a new series of values (candle).

  int index;
                    int LastIndexChecked = 0;
                    double sum = 0;

for(int barIndex = LastIndexChecked; barIndex <= CurrentBar; barIndex++)
                    {
                    double closePrice16 = Bars.GetClose(barIndex);
                    double openPrice9 = Bars.GetOpen(barIndex+1);
                    double closePrice = Bars.GetClose(barIndex+1);

                    double difCP = closePrice16 - openPrice9;

                    index = barIndex;


                    sum += difCP;

                    double candle = closePrice + sum;



                         Value[0] = candle;

I think somehow a break should be introduce to stop the loop at 513. Every minute a new bar is added in the chart, making the chart bars increase to 514, 515 ...

After a moment my indicator vanish from the chart. If i introduce a break after Value[0] i get a straight line in the chart.

Anyone has an idea where and how to introduce the break statement, if only that is the solution to my problem. Could be Value[0] as well but i am not sure what it is going to change.

Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,905 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,421 Reputation points
    2021-03-01T19:13:39.07+00:00

    You can set a break-point, hover over the break-point for the following
    73141-f1.png

    Click the gear and set a condition and under Actions uncheck "Continue Execution".

    73110-f2.png

    When hit, hover over variables to see what the problem is.

    1 person found this answer helpful.
    0 comments No comments

  2. Sam of Simple Samples 5,541 Reputation points
    2021-03-03T19:48:42.107+00:00

    You should use try {} catch {}. In the catch block you can show relevant information. You could then continue the program or exit the program. Tell us if you need details.

    Use of try {} catch {} is very useful for diagnostic purposes but it should be used for production code too.


  3. Karen Payne MVP 35,421 Reputation points
    2021-03-03T21:03:56.473+00:00

    Hello,

    The following are basic/simple which you should be able to use in tangent with my first reply or without my first reply.

    In regards to try/catch

    Basic try/catch

    try  
    {  
        var fileContents = File.ReadAllLines("NonExistingFile.txt");  
    }  
    catch (Exception exception)  
    {  
        MessageBox.Show($"We encountered issues\n{exception.Message}");  
    }  
    

    Using named tuples

    private (List<string> lines, bool success, Exception exception) ReadSomeFile()  
    {  
        List<string> contents = new List<string>();  
      
        try  
        {  
            var fileContents = File.ReadAllLines("NonExistingFile.txt");  
            return (lines: contents, success: true, exception: null);  
        }  
        catch (Exception ex)  
        {  
            return (lines: contents, success: false, exception: ex);  
        }  
      
    }  
    

    Usage

    var (lines, success, exception) = ReadSomeFile();  
    if (success)  
    {  
        foreach (var line in lines)  
        {  
              
        }  
    }  
    else  
    {  
        MessageBox.Show($@"Encountered issues {exception.Message}");  
    }  
    

    Logging

    This is a homebrewed logging class and there are many loggers on the web, this one keeps things simple that knows were the exception was but not the line number. I keped it simple on purpose.

    using System;  
    using System.Diagnostics;  
    using System.Runtime.CompilerServices;  
      
    namespace HandleExceptionsSimple  
    {  
        public static class Logger  
        {  
            private static TextWriterTraceListener _textWriterTraceListener;  
            public static void CreateLog()  
            {  
                _textWriterTraceListener = new TextWriterTraceListener(  
                    "applicationLog.txt",   
                    "PayneListener");  
                  
                Trace.Listeners.Add(_textWriterTraceListener);  
            }  
            public static void Close()  
            {  
                _textWriterTraceListener.Flush();  
            }  
            public static void Exception(string message, [CallerMemberName] string callerName = null) =>   
                WriteEntry(message, "error", callerName);  
              
            public static void Exception(Exception ex, [CallerMemberName] string callerName = null) =>   
                WriteEntry(ex.Message, "error", callerName);  
              
            public static void Warning(string message, [CallerMemberName] string callerName = null) =>   
                WriteEntry(message, "warning", callerName);  
              
            public static void Info(string message, [CallerMemberName] string callerName = null) =>   
                WriteEntry(message, "info", callerName);  
              
            public static void EmptyLine() => _textWriterTraceListener.WriteLine("");  
      
            private static void WriteEntry(string message, string type, string callerName)  
            {  
                _textWriterTraceListener.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss},{type},{callerName},{message}");  
      
            }  
        }  
    }  
    

    Usage

    try  
    {  
        var fileContents = File.ReadAllLines("NonExistingFile.txt");  
    }  
    catch (Exception exception)  
    {  
        Logger.Exception(exception);  
        Logger.Close();  
    }  
    

    Then there is global unhandled exception handling, see my NuGet package where documentation is under Prerequires.
    73907-f1.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.