0

In the constructor method of my "Translator"-Class, i need to initialize a ResourceManager to access all the strings i later need. But as i try to get the current executing Assembly, it fails with System.NullReferenceException occurred HResult=0x80004003 Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. Source=MyStreamingPlayer

The following Code is my Class Constructor:

class Translator
{
    public Form[] FormsToTranslate { get; }
    ResourceManager rm;

    public Translator(Form form, string language)
    {
        FormsToTranslate[0] = form;
        Assembly asm = Assembly.GetExecutingAssembly();

        string resname = asm.GetName().Name + ".lang_" + language;
        rm = new ResourceManager(resname, asm);
    }
}

And Assembly asm = Assembly.GetExecutingAssembly(); being the line failing with the System.NullReferenceException. A few hours ago i made some changes, the following Code worked fine:

class Translator
{
    public Form FormToTranslate { get; set; }
    ResourceManager rm;

    public Translator(Form form, string language)
    {
        FormToTranslate = form;
        Assembly asm = Assembly.GetExecutingAssembly();
        string resname = asm.GetName().Name + ".lang_" + language;
        rm = new ResourceManager(resname, asm);
    }
}

I don't know what happened, but if i change this part back to what i had before, everything works fine. As far as i can see, i didn't change anything that could cause Assembly.GetExecutingAssembly() to break with a NullReferenceException.

  • 3
    `GetExecutingAssembly` is not throwing an exception. `FormsToTranslate[0]` is throwing the exception, since you never initialized `FormsToTranslate` with a value. – BJ Myers Feb 05 '17 at 16:42
  • You're right, i missed that totally... Thanks! – MrElliwood Feb 05 '17 at 16:55

0 Answers0