Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Unhandled Exception.  Object reference not set to an instance of an object.

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

    Unhandled Exception.  Object reference not set to an instance of an object.

    Hello NT,

    I am getting an error message that flashes, "Unhandled Exception. Object reference not set to an instance of an object." It only stays up for a moment and then the program shuts down including the Output box. I had to simulate the conditions several times to read the entire message. It seems to only happen on certain instruments. Others are OK.

    How can I correct this?
    Thanks,
    Ben

    #2
    Hello Ben,

    Is this error coming from a custom NinjaScript Strategy you are developing in C# or one that you have imported from a 3rd party?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea. It's a bit of both. I imported the strategy from a third party and modified it to get it running. I added a couple of parameter property declarations that were not included.

      Comment


        #4
        Hello harr5754,

        You will need to identify what code in the script is causing the error.

        Comment out all of the logic and all of the custom variables. (Add to // slashes to the front of each line. Leave the declarations of OnStateChange() and OnBarUpdate().
        Check that the error no longer occurs.
        Then uncomment each variable one at a time.
        Check the error is still not occuring.
        Then uncomment each line of logic one at a time.
        What is the last line uncommented before the error returns?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          The last line I uncommented before the error was:

          Code:
           if (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx])
          EnterShort(Convert.ToInt32(DefaultQuantity), @"Entry01Short");
          Here are the variables and logic for the overall code:

          Code:
           entry1_sl = 5;
          entry1_slx = 20;
          }
          else if (State == State.Configure)
          {
          }
          }
          
          [NinjaScriptProperty]
          [Description("entry1_sl")]
          [Category("Parameters")]
          public int entry1_sl
          { get; set; }
          
          [NinjaScriptProperty]
          [Description("entry1_slx")]
          [Category("Parameters")]
          public int entry1_slx
          { get; set; }
          
          protected override void OnBarUpdate()
          {
          if (CurrentBar < BarsRequiredToTrade)
          return;
          
          
          if (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx])
          EnterLong(Convert.ToInt32(DefaultQuantity), @"Entry01Long");
          if (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx])
          EnterShort(Convert.ToInt32(DefaultQuantity), @"Entry01Short");
          }

          Comment


            #6
            Hello harr5754,

            These lines of code in OnBarUpdate would be surprising for a null variable error and not an index error.

            So if the rest of the code is uncommented and restored and then only these 2 lines are commented, the error does not occur correct?

            I see a possible indexing error.
            Close[entry1_slx]

            There is no check that CurrentBar is greater than entry1_slx. If the index is larger than the number of bars, this would cause an index error.
            https://ninjatrader.com/support/foru...13#post1048513
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea,

              I think the BarsRequiredToTrade may have been the problem. It was set at 21 and I was optimizing one of the parameters from 18 to 22. I reset it to 23 and it seems to be running without errors. A couple of questions:

              I guess this would be an index error. Could that have caused Ninjatrader program to shut down?

              Why would this happen on certain instruments and not others? I was running the same parameters on other instruments.

              Comment


                #8
                Hello harr5754,

                BarsRequiredToTrade would cause orders to be ignored if submitted before CurrentBar has reached BarsRequiredToTrade. This would not prevent the conditions from being evaluated, which start with CurrentBar 0.

                You would need to stop the logic processing by returning if CurrentBar is not greater than entry1_sl or entry1_slx.

                I am not expecting that this line would cause a null variable error (object not set to instance of object). I also would not expect NinjaTrader to crash. I am still wanting to confirm that when the one line is commented out the behavior stops, when the one line is uncommented the behavior can be reproduced, as I don't think that is the line of code causing the crash behavior.
                For this line I would expect the script to become disabled, an error to appear on the Log tab of the Control Center, but I would not expect NinjaTrader to crash and would expect the platform to keep running even after the script becomes disabled.

                I would expect an index error to occur, anytime entry1_sl or entry1_slx is used as a barsAgo index and has a value less than CurrentBar. This could be on any instrument.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chelsea, I could not isolate any particular line by commenting and uncommenting out the line. After I changed the BarsRequiredToTrade to 23 all of the instruments previously crashing the program were functioning. Now I have reset the BarsRequiredToTrade back to 21 and I am not able to replicate any crash conditions. I guess I'll have to wait until the crash conditions occur again since there is no way to troubleshoot it. Very strange.

                  Comment


                    #10
                    Hello Ben,

                    Thank you for your reply.

                    It may not be this strategy that is causing the error. Did you see this strategy name with the error on the Log tab of the Control Center?

                    Please send an email to platformsupport [at] ninjatrader [dot] com so that we may request your complete log and trace files.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Chelsea, the only appearance of a strategy name is when there is an index error. I sent you an email for the log and trace file request.

                      Thanks
                      Ben

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by frankthearm, Today, 09:08 AM
                      3 responses
                      7 views
                      0 likes
                      Last Post NinjaTrader_Clayton  
                      Started by yertle, Today, 08:38 AM
                      5 responses
                      15 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by adeelshahzad, Today, 03:54 AM
                      3 responses
                      16 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by bill2023, Yesterday, 08:51 AM
                      6 responses
                      27 views
                      0 likes
                      Last Post NinjaTrader_Erick  
                      Started by NinjaTrader_ChelseaB, 01-08-2017, 06:59 PM
                      80 responses
                      19,667 views
                      5 likes
                      Last Post NinjaTrader_ChelseaB  
                      Working...
                      X