Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error when calling misc method from OnBarUpdate

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

    Error when calling misc method from OnBarUpdate

    Hey guys,
    I am getting the "object reference not set to an instance of an object" error message when calling a Misc. Method from OnBarUpdate(). The method is quite simple, just loading an array with values, please see below.

    I have a line in OnBarUpdate() that says GenerateArray(); that's all

    Could you please give me a hint on where the issue might be?

    #region Methods
    private void GenerateArray()
    {
    //int [] array; //this one is already declared above in the variables region
    for(i=0;i<100;i++)
    {
    array[i]=j+multiplier;
    j=j+multiplier;
    //Print(array[i].ToString());
    }

    }
    #endregion

    #2
    Hello,

    Thank you for the question.

    There are a few errors here.

    First the Int[] you are using is not initialized, if you have done so in the script that is not here please ignore this.
    I added in

    Code:
    int [] array = new int[100];
    For an integer array or any type array you need to create the object in which the array variable is referencing. In this case I created a new int array with a count of 100 because that is how many times you are looping.

    Arrays need to allocate space when created so you need to know the amount of entries that is going into this. if you need something that does not need to know before hand on how many entries there will be use a DataSeries



    Next there is no variable in your for loop which is required in C#.

    you currently have:

    Code:
    for(i=0;i<100;i++)
    this would need to be
    Code:
    for([B]int [/B]i=0;i<100;i++)
    to be valid syntax.i needs to be defined before you can use it

    Also the variables j and multiplier have not been defined so I have added variable definitions for those as well. Here is the complete statement in valid syntax:

    Code:
    private void GenerateArray()
    {
         var array = new int[100];
         int j = 0;
         int multiplier = 0;
         for (int i = 0; i < 100; i++)
         {
              array[i] = j + multiplier;
              j = j + multiplier;
              Print(array[i].ToString());
         }
    }
    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Ok, so I was able to load the values in the array, but I am getting the same error message when using Print() from OnBarUpdate() to check the values from OnBarUpdate():

      for (int i = 0; i < 100; i++)
      {
      Print("Array" + array[i].ToString());
      }

      Comment


        #4
        Should I use "return array;" inside the method and make it a int[] type of method?

        Comment


          #5
          Hello,

          Thank you for the additional information.

          Can you please post your code that you have or a working example that demonstrates what you are doing with all variables you are using and the error?

          The statement
          Code:
          for (int i = 0; i < 100; i++)
          {
          Print("Array" + array[i].ToString());
          }
          is valid syntax but this would not matter if array is not yet initialized or has not been assigned values yet.

          I am unsure of where the error is exactly as the code posted seems valid.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            In summary it would be what is shown below. I would like to see (Print) the array loaded from OnBarUpdate() (called on Initialize() so it should be loaded)

            Code:
                    #region Variables
                    // Wizard generated variables
                    private int multiplier = 50; 
                    private int [] array;
                    #endregion
            
            protected override void Initialize()
                    {
                    int [] array = new int[100];
            	CalculateOnBarClose = false;
            	GenerateArray();
            	}
            
            protected override void OnBarUpdate()
            {
            					
            	for (int i = 0; i < 100; i++)
            	{
            	Print("Array" + array[i].ToString());
            	}
            }
            
            #region Methods
            		private void GenerateArray()
            		{
            		var array = new int[100];
                 		int j = 0;
                 		//int multiplier = 0;
                 		for (int i = 0; i < 100; i++)
                 		{
                      		array[i] = j + multiplier;
                      		j = j + multiplier;
                      		//Print(array[i].ToString());
                 		}
            			
            			
            		}

            Comment


              #7
              Hello,

              Thank you this helps.

              The reason you have an error is that you are re declaring a variable.
              It looks like you had defined the array in your variables section as a new variable.
              You had also re defined the same name in your private method.

              you had:
              Code:
              var array = new int[100];
              but it needed to be
              Code:
              array = new int[100];
              When you define a class scope variable in the #region Variables area, this will allow you to use that variable throughout all of your methods inside the parent class which is your indicators class.

              Using var you have told C# that this is not associated with the defined variable in the variables section and is its own variable. this never gets set how it is. I have modified the script and re posted it, I did verify this compiles and runs.

              I also removed the un needed assignment in Initialize as you are Initializing this variable every time you call the method which clears the prior list before it starts.

              Code:
              private int[] array;
                      private int multiplier = 50;
              
                      protected override void Initialize()
                      {
                          CalculateOnBarClose = false;
                          //use OnStartUp instead
                      }
              
                      protected override void OnStartUp()
                      {
                          GenerateArray();
                      }
              
                      protected override void OnBarUpdate()
                      {
                          for (int i = 0; i < 100; i++)
                          {
                              Print("Array" + array[i]);
                          }
                      }
              
                      private void GenerateArray()
                      {
                         [B] array = new int[100];[/B]
                          int j = 0;
              
                          //int multiplier = 0;
                          for (int i = 0; i < 100; i++)
                          {
                              array[i] = j + multiplier;
                              j = j + multiplier;
              
                              //Print(array[i].ToString());
                          }
                      }
              Additionally you want to avoid using Initialize for your own code as this gets called when you open the indicators menu.

              You should use OnStartUp instead or:

              Code:
              protected override void OnStartUp() 
              {
              
              }
              This gets called when the script starts but only after it is applied to the chart, not in the indicators menu.
              I look forward to being of further assistance.
              Last edited by NinjaTrader_Jesse; 12-11-2014, 04:51 PM.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              18 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              1 view
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              6 views
              0 likes
              Last Post Javierw.ok  
              Started by timmbbo, Today, 08:59 AM
              2 responses
              10 views
              0 likes
              Last Post bltdavid  
              Started by alifarahani, Today, 09:40 AM
              6 responses
              41 views
              0 likes
              Last Post alifarahani  
              Working...
              X