-
Nov 13th, 2017, 11:10 PM
#1
Thread Starter
Addicted Member
[RESOLVED] TypeInitializationException thrown using an object from a module
I was adding a bunch of code and didnt commit frequently and now I have a runtime error bug.
An unhandled exception of type 'System.TypeInitializationException' occurred in RSG.exe
Additional information: The type initializer for 'RSG.Classes' threw an exception.
Code where the execution happens. frmStart is the start up form.
Code:
Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text += Application.ProductVersion
frm.Show() 'EXCEPTION IS THROWN HERE.
End Sub
Code for module
Code:
Module Classes
Public frm As New frmStringGen
End Module
Load method of the frmStringGen object.
Code:
Code actually never reaches this point and it doesn't contain an InitializeComponent() method in it. It didn't happen before.
I want to mention some more information. I am using My.Settings stuff and also my app.config has been reset. I tried to lower the application framework from 4.5.2 to 4 and that didn't work either.
I am stuck and need a bit of walking through at this point. I can't find the bug.
app.config file receives 38 messages like so.. repeating through each setting implementation
Could not find schema information for the element 'setting'.
Could not find schema information for the attribute 'name'.
Code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="RSG.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<userSettings>
<RSG.My.MySettings>
<setting name="bgSelection" serializeAs="String">
<value>0</value>
</setting>
<setting name="optClip" serializeAs="String">
<value>True</value>
</setting>
<setting name="optBigInt" serializeAs="String">
<value>False</value>
</setting>
<setting name="optBound" serializeAs="String">
<value>True</value>
</setting>
<setting name="optGlue" serializeAs="String">
<value>False</value>
</setting>
<setting name="optRndMethod" serializeAs="String">
<value>1</value>
</setting>
<setting name="optCheckUpdatesOnLoad" serializeAs="String">
<value>False</value>
</setting>
<setting name="version" serializeAs="String">
<value>2.0</value>
</setting>
<setting name="scrollErrorShown" serializeAs="String">
<value>False</value>
</setting>
</RSG.My.MySettings>
</userSettings>
</configuration>
Last edited by Reapism; Nov 13th, 2017 at 11:34 PM.
-
Nov 13th, 2017, 11:47 PM
#2
Re: TypeInitializationException thrown using an object from a module
It's not the Load event handler that's the problem. What is almost certainly happening is that one of the property values that you've set in the designer is causing an event to be raised and then the handler for that event is doing something invalid. For instance, if you set the Text property of a TextBox then the TextChanged event will be raised during construction. If that event handler assumes that something has been done that actually hasn't at that stage then an exception could be thrown. If you actually look at the stack trace of the exception, you may be able to see exactly where it's originally thrown. If not, at least you the sort of thing to look for.
-
Nov 14th, 2017, 05:59 PM
#3
Thread Starter
Addicted Member
Re: TypeInitializationException thrown using an object from a module
I did a stack trace using the try and catch function.
Code:
Try
frm.Show()
Catch ex As Exception
Clipboard.SetText(System.Environment.StackTrace)
End Try
Here is what it threw.
Code:
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at RSG.frmStart.frmStart_Load(Object sender, EventArgs e) in E:\All Files\Programs\_Git\Random String Generator\Random String Generator\frmStart.vb:line 20 'Here is where i show the form frm.Show()
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at RSG.My.MyApplication.Main(String[] Args) in :line 81
I should also note that I renamed the application recently but I wasn't getting this error before, only now.
I changed the Root name and assembly name to RSG.
Data:
Code:
{System.Collections.ListDictionaryInternal}
' This is probably one of the settings I've added but removed to try to debug
The inner exception is:
Code:
{"Object reference not set to an instance of an object."}
TargetSite:
Code:
{Void frmStart_Load(System.Object, System.EventArgs)}
Last edited by Reapism; Nov 14th, 2017 at 06:06 PM.
-
Nov 14th, 2017, 06:54 PM
#4
Re: TypeInitializationException thrown using an object from a module
In a Catch block, just call ToString on the exception to get all the relevant information. You could also have simply clicked the View Details link on the Exception Assistant window and got the stack trace from there.
If you put a breakpoint on the line that 'frm.Show' call, is 'frm' Nothing there?
I have to question why you have that module at all though. It seems like you should simply use the default instance there or a true singleton. What you have doesn't appear to be of any value.
-
Nov 14th, 2017, 08:55 PM
#5
Thread Starter
Addicted Member
Re: TypeInitializationException thrown using an object from a module
There are many classes that need to access an object reference instead of the true singleton. I had limitations doing some executions on just single instances. But with the module, I can call frm anywhere its its helpful to me. I guess a better explanation is I didn't want a static reference. But anyways, I had this same code doing the same thing for about a year and I must have done something and it now it throws this exception. In the data part of the exception viewing detail, it said something about another form? Would that have anything to do with it somehow?
To answer your question, frm shouldn't be nothing there because i instantiate it in the module with the new keyword
Code:
Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text += Application.ProductVersion
frm = New frmStringGen 'This line also throws the same exception.
frm.Show()
End Sub
Module code:
Code:
Module Classes
Public frm As New frmStringGen
End Module
Last edited by Reapism; Nov 14th, 2017 at 09:03 PM.
Reason: added more information
-
Nov 14th, 2017, 09:32 PM
#6
Re: TypeInitializationException thrown using an object from a module
To answer your question, frm shouldn't be nothing there because i instantiate it in the module with the new keyword
That really doesn't answer the question. We know it shouldn't be Nothing but have you actually checked?
-
Nov 14th, 2017, 09:48 PM
#7
Thread Starter
Addicted Member
Re: TypeInitializationException thrown using an object from a module
Originally Posted by wes4dbt
That really doesn't answer the question. We know it shouldn't be Nothing but have you actually checked?
No code changed in the frmStringGen to make it nothing, the problem lies somewhere else likely. I did check, but code didn't really change there.
-
Nov 14th, 2017, 10:22 PM
#8
Re: TypeInitializationException thrown using an object from a module
We're not asking you whether any code has changed. We're asking you whether that variable is Nothing when that line of code is executed. Please answer that question. Put a breakpoint on that line and mouse over the variable to see its value when it gets hit. Is it Nothing or not? I'm not asking this for my health. If that variable is Nothing then we need to address that specifically. If it's not then something somewhere else is Nothing and that's a completely different problem.
-
Nov 14th, 2017, 10:28 PM
#9
Re: TypeInitializationException thrown using an object from a module
Originally Posted by Reapism
There are many classes that need to access an object reference instead of the true singleton. I had limitations doing some executions on just single instances. But with the module, I can call frm anywhere its its helpful to me. I guess a better explanation is I didn't want a static reference.
That doesn't make any sense. Both a singleton and a default instance are object references. I think you're trying to solve a problem that doesn't exist.
Originally Posted by Reapism
In the data part of the exception viewing detail, it said something about another form? Would that have anything to do with it somehow?
It could have something to do with it but if you're not going to tell us what it actually said then we can only guess. We aren't psychic. We only know what you tell us, so you need to tell us everything that's relevant.
-
Nov 15th, 2017, 10:22 AM
#10
Re: TypeInitializationException thrown using an object from a module
Actually I'm surprised no expert has mentioned this:
TypeInitializationException is sort of useless on its own. It tells you "another exception was thrown while I was doing something in a type initializer". Not to get bogged down, but that means the important part is the "another exception".
It has an InnerException property that will tell you what the "another exception" is. That will tell you precisely what went wrong and where. Looking at the TypeInitializationException itself won't tell you that.
Something like this might work better:
Code:
Try
frm.Show()
Catch ex As Exception
If ex.InnerException IsNot Nothing
Debug.WriteLine(ex.InnerException)
Else
Debug.WriteLine(ex)
End If
End Try
It is true that TIE means something has gone wrong in a static context, probably a Module initializer. The inner exception will tell you which line failed.
Please don't get in the habit of trying to find the ONE LINE that you think we need to see. Often the cause of an error involves the 5-10 lines around that one line. When in doubt, post too much code but point us to the line that fails.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 15th, 2017, 04:26 PM
#11
Thread Starter
Addicted Member
Re: TypeInitializationException thrown using an object from a module
@Sitten, thank goodness you mentioned that. It pointed to the other form that i failed to mention before sorry @jmc and @wes. I was just unable to produce where that error was coming from, once i saw what code was causing it. I fixed within seconds. Thank you all for your advice.
ANYONE WITH THIS ERROR, the error is caused by another exception thrown elsewhere in your project. To find what is causing your error, use a Try block on the statement that's initially throwing the exception, then in the catch block write anything that can display the inner exception.
Reference @sittens post above.
-
Nov 15th, 2017, 05:36 PM
#12
Re: TypeInitializationException thrown using an object from a module
If you call ToString on the exception that is thrown, as I suggested in post #4, then it should include information from the inner exception as well, although I'm not sure that that is always the case. In the end, did it turn out to be a property that you set in the designer causing an event handler to execute some code that was making an invalid assumption, as I suggested in post #2?
-
Nov 15th, 2017, 06:34 PM
#13
Re: TypeInitializationException thrown using an object from a module
Originally Posted by jmcilhinney
If you call ToString on the exception that is thrown, as I suggested in post #4, then it should include information from the inner exception as well, although I'm not sure that that is always the case.
This is correct, usually the format is like:
Code:
TypeIntializationException blah blah blah
<call stack>
Inner exception: NullReferenceException blah blah blah
<call stack>
Seemed like your request was getting lost in translation somewhere, so I figured explicitly explaining the inner exception might help.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 17th, 2017, 11:54 AM
#14
Thread Starter
Addicted Member
Re: TypeInitializationException thrown using an object from a module
What it was instantiating an array which for some reason had so much problems. I will post that code here.
I never really use arrays in VB. So while I was looking up the documentation for it, I tried to instantiate the eighth element to empty string without
instantiating the ones before and oh boy. The code is stupid and doesn't do anything useful.
Code:
dictURLs(7) = ""
For Each i As String In dictURLs
If IsNothing(i) Then
MsgBox("Nothing")
Else
MsgBox(dictURLs(i))
End If
Next
-
Nov 17th, 2017, 11:55 AM
#15
Thread Starter
Addicted Member
Re: TypeInitializationException thrown using an object from a module
Definitely did. When I called the ToString it didn't display innerexception details to me as jmc mentioned.
-
Nov 17th, 2017, 02:05 PM
#16
Re: [RESOLVED] TypeInitializationException thrown using an object from a module
Let's make it real clear.
I see way back in #3 you actually did know about the InnerException, but you didn't quite get how to get the right information about it. I'm going to write an application that on-purpose throws TypeInitializationException, and show you what the output looks like. Maybe that'll save us a few more rounds of back and forth post-resolution.
Here's the application:
Code:
Public Class Form1
Protected Overrides Sub OnShown(e As EventArgs)
MyBase.OnShown(e)
Try
Dim frm As New Form2()
frm.Show()
Catch ex As Exception
Debug.WriteLine(ex)
End Try
End Sub
End Class
Public Class Form2
Inherits Form
Public Shared NotSomething As String = Nothing
Public Shared WillFail As String = NotSomething.Length.ToString()
End Class
"Type Initialization" is a fancy word for "running a static constructor" which is a concept VB uses completely different words to refer to. It's like "the Sub New for a Module" but for a class is like "Shared Sub New()" and automatically happens if you have some variables to initialize. So I gave Form2 some Shared variables and made sure there's no valid way for them to be set.
Here's what it outputs:
Code:
Exception thrown: 'System.NullReferenceException' in WindowsApp38.exe
Exception thrown: 'System.TypeInitializationException' in WindowsApp38.exe
System.TypeInitializationException: The type initializer for 'WindowsApp38.Form2' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsApp38.Form2..cctor() in C:\Users\[redacted]\Documents\Visual Studio 2017\Projects\WindowsApp38\WindowsApp38\Form1.vb:line 21
--- End of inner exception stack trace ---
at WindowsApp38.Form2..ctor()
at WindowsApp38.Form1.OnShown(EventArgs e) in C:\Users\[redacted]\Documents\Visual Studio 2017\Projects\WindowsApp38\WindowsApp38\Form1.vb:line 7
This is easy to read when you've seen it a thousand times.
The NullReferenceException was thrown first, but Visual Studio wasn't configured to do anything about it so my app kept going.
The framework caught it and wrapped it in a TypeInitializationException for me, but VS wasn't configured to do anything about that either.
My Catch block caught it and printed the message, which says in order:
I got a TypeInitializationException that was spawned by a NullReferenceException. That NullReferenceException was thrown from a Shared Sub New (That's what ".cctor()" means) in Form1.vb, line 21. The TypeInitializationException was thrown from Form2's constructor (".ctor()") in response to being called by Form1.OnShown() on line 7.
So the real culprit is Form1.vb, line 21:
Code:
Public Shared WillFail As String = NotSomething.Length.ToString()
And we know why that failed since we intended for it to fail.
A bad habit is to look at just the Message property of an exception, the full ToString() is about the most useful thing you can look at! The debugger doesn't tend to display the whole thing at the same time, you have to dig in the little VS dialogs to see the good stuff.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 17th, 2017, 06:07 PM
#17
Thread Starter
Addicted Member
Re: [RESOLVED] TypeInitializationException thrown using an object from a module
Clarified to the extreme. This thread should be the staple for which people like myself seek to resolve their exception. I can't give either of you any more rep, but I certainly can thank you all for your posts. Such an error has prevented me from working on my app for five days, I am very grateful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|