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

Object reference not set to an instance of an object.

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

    Object reference not set to an instance of an object.

    I've made a class with a simple function for now, but when I try to use that function, I get Object reference not set to an instance of an object. error
    posted a screenshot of the code and the error.
    My question is how can I make my own class of functions and then use that in my indicators, can I also make the class in one indicator, and use it in all of the other indicators without the need for the one containing the classes being active on the chart?

    Click image for larger version

Name:	image_78424.png
Views:	147
Size:	76.4 KB
ID:	1234917

    #2
    Hello LuxSpuzy,

    You can use a partial class of the Indicator class in a separate file to make a class available to all indicators.

    Below is a link to an example.
    https://ninjatrader.com/support/foru...ow#post1142340

    In your script you have ta declared as the type pine, but the ta variable is never instantiated as a new object instance of the pine class. So this is an empty variable holding no object.

    You need to instantiate the ta variable.
    Code:
    else if (State == State.DataLoaded)
    {
        ta = new pine();
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you very very much

      Comment


        #4
        if I try to export my files that use functions from the partial class files I get an error
        The type or namespace name 'pine' could not be found (are you missing a using directive or an assembly reference?)
        I've tried to do "using" but I guess I've used it wrongly ​

        EDIT: I've managed to make it work by making a new directory in Indicators named Pine, then I do

        using NinjaTrader.NinjaScript.Indicators.Pine;

        and the file inside that folder has to be named the same as the class inside it.

        and now the indicator works and I can export it correctly as well.

        but now I have

        Code:
                protected pineLib pine;​
        
                else if (State == State.DataLoaded)
                {
                    pine = new pineLib();
                }
        
                double ph = pine.pivothigh(High,5,5,CurrentBar);​​


        but how can I now make a sub class inside so that I can have my functions organized better for example

        Code:
                protected pineLib pine;​
        
                else if (State == State.DataLoaded)
                {
                    pine = new pineLib();
                }
        
                double ph = pine.ta.pivothigh(High,5,5,CurrentBar);​​
                double max= pine.math.max(1,2);​​​


        how can I make it so I have ta and math subclasses (not sure if that is called a subclass or what but I think you get the point of my question?)
        Last edited by LuxSpuzy; 02-15-2023, 06:52 AM.

        Comment


          #5
          Hello LuxSpuzy,

          To confirm, you've made a partial class of the Indicator class correct? (So that your class is available to all Indicators)

          Make sure you add the file with the partial class to your export. Check the boxes for the partial class file and your indicator file.

          In your custom class you can have further classes.

          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
              public partial class Indicator
              {
                  public class PineLib
                  {
                      public MyInnerClass InnerClassInstance;
          
                      public PineLib()
                      {
                          InnerClassInstance = new MyInnerClass();
                      }
          
                      public class MyInnerClass
                      {
                          public MyInnerClass()
                          {
                              DoubleValue = 5.1;
                          }
          
                          public double DoubleValue;
          
                          public double DoubleMethod()
                          {
                              return 6.2;
                          }
                      }
                  }
              }
          }
          private PineLib pine;

          pine = new PineLib();
          Print(pine.InnerClassInstance.DoubleValue);
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            thank you, that works
            but now I can't use the function CrossAbove inside my functions inside the class

            I get the error
            "Cannot access a non-static member of outer type 'NinjaTrader.NinjaScript.NinjaScriptBase' via nested type 'NinjaTrader.NinjaScript.Indicators.Indicator.pine Lib.TechnicalAnnalysis"

            sorry for so many questions, I am new to C# and learning still
            Last edited by LuxSpuzy; 02-15-2023, 03:51 PM.

            Comment


              #7
              Hello LuxSpuzy,

              As you are not placing the methods in the Indicator partial class, you will need to supply your indicator instance to the sub class if you want to use methods and properties from the indicator base.

              Code:
              namespace NinjaTrader.NinjaScript.Indicators
              {
                  public partial class Indicator
                  {
                      public class PineLib
                      {
                          public MyInnerClass InnerClassInstance;
              
                          public PineLib(NinjaScriptBase thisOwner)
                          {
                              InnerClassInstance = new MyInnerClass(thisOwner);
                              thisOwner.Print("hello");
                          }
              
                          public class MyInnerClass
                          {
                              private NinjaScriptBase owner;
                              public MyInnerClass(NinjaScriptBase thisOwner)
                              {
                                  DoubleValue = 5.1;
                              }
              
                              public double DoubleValue;
              
                              public double DoubleMethod()
                              {
                                  if (owner.CrossAbove(owner.Close, owner.Open, 1))
                                      owner.Print("crossed above");
                                  return 6.2;
                              }
                          }
                      }
                  }
              }​


              Do you need these to be classes or is this just for organizing methods?

              With the class do you an instance of the class because it going to store values or are you just needing to call methods?

              While static classes and methods would be technically unsupported, it might be what you need.

              Below is a link to an example.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                ah okay, I kinda understand, it works now, I've put this in the class constructor, it works and hope this is fine
                Code:
                public TechnicalAnalysis(NinjaScriptBase thisOwner)
                {
                    owner = thisOwner;
                }​
                just for organizing methods, I'm trying to make a library of pine functions so that converting will be easier
                just for calling the methods, won't be storing any values within the classes

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by morrnel, Today, 06:07 PM
                0 responses
                2 views
                0 likes
                Last Post morrnel
                by morrnel
                 
                Started by thumper57, Yesterday, 04:30 PM
                6 responses
                19 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by sastrades, 05-10-2024, 09:59 AM
                3 responses
                54 views
                0 likes
                Last Post rc5781
                by rc5781
                 
                Started by guyonabuffalo, Yesterday, 10:01 PM
                2 responses
                20 views
                0 likes
                Last Post guyonabuffalo  
                Started by reynoldsn, 05-10-2024, 07:04 PM
                5 responses
                27 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Working...
                X