none
The type initializer for ProgramName.Module1 threw an exception

    Question

  •  

    I am having a problem with public variables which are named in a Module.  They work fine on the PC that I have developed the application in but if I run the application on any other PC I receive an error “The type initializer for ProgramName.Module1 threw an exception".  I was able to step through the program and discovered that the first public variable that I am using does not seem to be working or active at the time that the first form loads and attempts to use the public variable.  The code looks like this.

     

    Try

       FileOpen(1, "C:\Program fles\ProgramName\bin\defaultpath.txt", OpenMode.Input)

       DefaultPath = LineInput(1)

     

       FileClose(1)

    Catch ex As Exception

       MsgBox("Error opening File.  Error = " & ex.Message)

       Stop

    Finally

       FileClose(1)

    End Try

     

    DefaultPpath is my Public variable declared in Module1.  If I change the code to the following adding a Dim statetement for the variable DefaultPath it works fine but then errors the next time DefaultPath is attempted to be used:

     

    Dim DefaultPath As String

    Try

       FileOpen(1, "C:\Program fles\ProgramName\bin\defaultpath.txt", OpenMode.Input)

       DefaultPath = LineInput(1)

     

       FileClose(1)

    Catch ex As Exception

       MsgBox("Error opening File.  Error = " & ex.Message)

       Stop

    Finally

       FileClose(1)

    End Try

     

     

    Thanks in advance


    bspiro
    Saturday, August 02, 2008 2:41 PM

Answers

  •  Looking at the Module1 I'd say the real error lies in the constructor for the Form_Modify_Data or the Form_MainMenu. The reason for this is that when the first member of Module1 is accessed then the module is instantiated (actually its a class...) and the forms you declared are constructed.
    Since the DefaultPath is the first member accessed in Module1 this will trigger the instantiation of Module1 and any error that occurs while constructing the Module will be encapsulated in an InvalidOperationException with the message: The type initializer for ProgramName.Moule1 threw an exception".

    So, to find the true error you will need to use the debugger in Visual Studio and work your way down the InnerException properties until you find the real cause. This is rather easy. Make sure to set the build to Debug. When the exception gets thrown a messagebox will turn up. Under "Action" choose "View details...". Expand the exception in the second window and then expand the InnerException node for as many times as possible (when you reached the root error the InnerException would read "Null").

    If you want more help in resolving the error please post the Root error message and Root error Stack trace.
     
    /Calle
    - Still confused, but on a higher level -
    • Marked as answer by bspiro Monday, August 04, 2008 4:15 PM
    Monday, August 04, 2008 9:22 AM

All replies

  •  In your second code snippet you are actually not accessing the Modules DefaultPath member since you have declared a local variable with the same name.

    If you could provide the full source code for the Module1 this would help since the code snippets you provided does not really tell us anything about the source of the error.

     

    /Calle


    - Still confused, but on a higher level -
    Sunday, August 03, 2008 8:40 PM
  • Yes in the second example I added the Dim statetement just to show that the problem was that the public variables in Module1 were not initialized.  Once I did this the type intialization error went away but later in the program when I again reference DefaultPath I receive an error becaus the local variable is no longer available.

    In Module1 all I have are public variable and I also make public most of the forms so that I can pass variables between the different forms.  Below is a sample of what is in Module1, there is nothing else other than public variables and public forms. 

    I have seen a number of references to this same error but no resolutions.  Also as mentioned this code all works fine on the computer that I did the development on.  But If I take the same code to another computer with VS.NET 2005 it loads and compiles fine but gives the error mentioned.

    Module Module1

    Public msg As String 'Used for various messages

    Public LastFile As String 'Used to hold the last file saved

    Public DefaultPath As String 'Used for the default path for the application files


    Public
    FormMod As New Form_Modify_Data

            Public FormMain As New Form_Main_Menu
    End Module

     

     


    bspiro
    Sunday, August 03, 2008 9:59 PM
  •  Looking at the Module1 I'd say the real error lies in the constructor for the Form_Modify_Data or the Form_MainMenu. The reason for this is that when the first member of Module1 is accessed then the module is instantiated (actually its a class...) and the forms you declared are constructed.
    Since the DefaultPath is the first member accessed in Module1 this will trigger the instantiation of Module1 and any error that occurs while constructing the Module will be encapsulated in an InvalidOperationException with the message: The type initializer for ProgramName.Moule1 threw an exception".

    So, to find the true error you will need to use the debugger in Visual Studio and work your way down the InnerException properties until you find the real cause. This is rather easy. Make sure to set the build to Debug. When the exception gets thrown a messagebox will turn up. Under "Action" choose "View details...". Expand the exception in the second window and then expand the InnerException node for as many times as possible (when you reached the root error the InnerException would read "Null").

    If you want more help in resolving the error please post the Root error message and Root error Stack trace.
     
    /Calle
    - Still confused, but on a higher level -
    • Marked as answer by bspiro Monday, August 04, 2008 4:15 PM
    Monday, August 04, 2008 9:22 AM
  • Calle,
     
    I found the issue late last night doing pretty much exactly what you said.  The issue was not in Module1 at all, also like you indicated.  The real problem was a hard coded directory for a WAV file which I had put in the program about 1 1/2 years ago.  At the time I did not know how to derive the path for the program.  This is why it would fail whenever I used the app on another computer. 

    If I hadn't figured this out late last night what you described in your response would have led me to the solution though, so I do appreciate your help.
    bspiro
    Monday, August 04, 2008 4:15 PM