Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Object reference error while using Reflection

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

    Object reference error while using Reflection

    Hello,
    I am following the instructions in the guide for Considerations for Compiled Assemblies, which I'm using. I'm using Reflection as it suggests and everything works fine on my dev machine. However, when I compile it to a protected assembly and try on my other laptop, I receive an error "Object reference not set to an instance of an object". I have used logging to find the exact line at which this happens. First I have this line, which works fine:

    System.Type type = System.Type.GetType("NinjaTrader.NinjaScript.Chart Styles.OhlcStyle"); // NOTE: this forum is putting a space between ChartStyles, but this space is NOT in the code.

    However, when I try to access the "type" object, I receive the error. This works just fine on my dev machine, so I have no idea why it's not working in a compiled assembly. I tried these to lines and they both failed with the same error:

    Print(type.ToString());
    and
    System.Reflection.Assembly assembly = type.Assembly;

    Please advise, as this seems like I'm following the exact instructions in the guide.
    Thank you!

    #2
    Hello pdennis,

    Welcome to the NinjaTrader support forum.

    From the provided syntax, I am not certain what may be happening other than the object being used is null. This is likely that the type cannot be found in the context being used, could you try the following syntax in place of GetType to see if this helps?

    Code:
    NinjaTrader.Core.Globals.AssemblyRegistry.GetType("NinjaTrader.NinjaScript.ChartStyles.OhlcStyle");
    You can find more of an example of this type of reflection in the following publicly available link: https://ninjatraderecosystem.com/use...le-identifier/

    If that is not the solution, I would likely need to test this further on my end. If you have a small existing sample that you can export the code for and attach it for me to test I would be happy to do that.


    As a side note, if you click the "A" in the forum text editors toolbar, you can access the "# Code" block if you want to paste some code on the forum.


    I look forward to being of further assistance.

    Comment


      #3
      Awesome, that fixed it! I'm fairly new to reflection, so can you help clarify why it worked on the dev machine but not in the compiled assembly? Thank you very much! Also great to know about the advanced editor for code blocks

      Comment


        #4
        Hello pdennis,

        I really couldn't say with certainty without trying the file you had in the same situation, this could be due to the way the file was coded potentially. If by the dev machine you mean you were running from source code, that is likely the reason as it was part of the NinjaTrader.Custom.dll opposed to being its own assembly when exported.

        In general, reflection is not a subject which is documented specifically for NinjaTrader as it is very infrequently used. This is just a C# concept that you can find much more information about online in C# educational materials. The method I had suggested is used to access *all* of the imported assemblies in NinjaTrader rather than the current assembly context which is the normal way in reflection that you had used.

        I look forward to being of further assistance.

        Comment

        Latest Posts

        Collapse

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