Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error: Object reference not set to an instance of an object

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Error: Object reference not set to an instance of an object

    Dear ladies and gentlemen,


    i have the following problem. I createt a class where i defined a couble of attributes. after that i instanced a Serie<> and the type is the name of the class with the attribute where i want to save all the values i need. But when i´d like to ask for the values[1] i alwas get this error.

    where is the problem. there shoud be saved value[0] and value[1].

    In the following the code.

    Candle = new Series<LevelAndre>(this, MaximumBarsLookBack.Infinite);
    }
    }

    protected override void OnBarUpdate()
    {
    if (startTime.Date != Time[0].Date)
    {
    startTime = new DateTime(Time[0].Year, Time[0].Month, myDate.Day, StartTime.Hour, StartTime.Minute, StartTime.Second);
    myDate = new DateTime(Time[0].Year, Time[0].Month, myDate.Day, StartTime.Hour, StartTime.Minute, StartTime.Second);
    }
    if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute)
    {
    if (CurrentBar < 1)
    {
    return;
    }

    if (BarsPeriod.Value == 60)
    {

    if (Time[0] == startTime)
    {
    LevelAndre level = new LevelAndre(High[0], Low[0], Open[0], Close[0], Time[0], BarsPeriod.BarsPeriodType, BarsPeriod.Value, CurrentBar);
    Candle[0] = level;
    Print(Candle[0].Starts.Month);


    if (Candle.Count >= 0 && count == false)
    {
    count = true;
    }
    else if (count)
    {
    Print(Candle[1].ID);
    Print("");
    Draw.Line(this, "Cost Afferage", true, CurrentBar - Candle[0].ID, Candle[1].Close, CurrentBar - Candle[0].ID, Candle[0].Close, Plots[0].Brush, Plots[0].DashStyleHelper, (int)Plots[0].Width, DrawOnPricePanel);
    }

    Thanks for your help in advance!
    Last edited by sane1111; 04-04-2022, 11:51 AM.

    #2
    Hello sane1111,

    Thanks for your post.

    Object reference not set to an instance of an object indicates that there is a null value in a variable being used in your script.

    Prints would need to be added to the script to the script to debug where the null value is in the script. After you find the exact object returning null, you could add a condition check in the script to check that the value is not null.

    For example, see below.

    Code:
    if (Currentbar > 0 && Candle[1] != null)
    {
        Print(Candle[1].ID);
    }
    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thanks for your respond, I´ve change my code now but its still the same. could it be possible, that the amount of a Series is to less? When i Print(Candle.count) the output is 5988.
      In addition Print(Candle[1] != null) is never true.



      Code:
      if (Currentbar > 0 && Candle[1] != null)
      {
      Print(Candle[1].ID);
      }
      Last edited by sane1111; 04-04-2022, 12:44 PM.

      Comment


        #4
        Hello sane1111,

        Thanks for your note.

        The code in my previous email was meant to be an example of checking if a value is not null in your script. It was not meant to be the resolution for the error.

        You would need to add prints to your script to locate the exact object that is null in your script. Once you've located the object, you could create a condition that checks if that value is not null similar to the example code.

        For information about using prints for debugging, please see the forum thread linked in my previous post.

        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Sorry for the missunderstanding, i now where i get the null exeption. always when i´d like to use something of Candle[1]. I debugged the code and made comment of the Code with Candle[1]. the debugger run to the end. the only problem i have is that can not use the Candle in the past.

          thanks for your help!!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          633 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          364 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          567 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          568 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X