-4

I am initializing an object from the class TagNameHandler in another class.

This class has a constructor:

enter image description here

Every time I try to initialize the object in another class, I receive this message:

enter image description here

Both classes are public and none of them are static.

How can I solve this?

Thanks.

Ryan H.
  • 2,543
  • 1
  • 15
  • 24
  • 5
    Please do not [post code as pictures](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – JSteward Sep 28 '18 at 21:39
  • 1
    there is a message right there telling you what went wrong. I cannot read it but I assume you can – pm100 Sep 28 '18 at 21:39
  • Might you please [edit] your question to include your code and exception as **text** rather than as a screenshot? It's policy here not to to use images for this purpose, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. – dbc Sep 28 '18 at 21:51
  • You may (or may not) need to use lazy singleton initialization. See http://csharpindepth.com/Articles/General/Singleton.aspx#nested-cctor and https://stackoverflow.com/questions/2550925/singleton-by-jon-skeet-clarification. Also see [What is and how to fix System.TypeInitializationException error?](https://stackoverflow.com/q/18989240) and [how to solve System.TypeInitializationException was unhandled exception in vb.net?](https://stackoverflow.com/q/7202543). The 2nd question is for vb.net but the answer applies to c# also. – dbc Sep 28 '18 at 21:54

1 Answers1

2

TypeInitializationException means that TagNameHandler threw an exception while initializing the type. By that, it means while assigning static variables and calling static constructors. The only static variable that is likely to throw an exception is the instance object, which calls the TagNameHandler constructor. You will have to view the InnerException of that exception to get anywhere, as so far there is minimal information. There should be a clickable "View Details" button to follow the stack trace.

Something quick would be if AppSettings doesn't contain one of those strings you're accessing. Perhaps debug print AppSettings first. Otherwise, it'll be in createTagDictionary(). The inner exception will tell you.

Nicholas Pipitone
  • 4,002
  • 4
  • 24
  • 39
  • TypeInitializationException doesn't mean that there was an exception in the constructor. It means that there was an exception when initializing the class's static properties. That being said, the exception likely is coming from when it calls the constructor for the "instance" property. – Eric Damtoft Sep 28 '18 at 21:52
  • @EricDamtoft Ah, thanks. Fixed it, this time looking it up on [MSDN](https://learn.microsoft.com/en-us/dotnet/api/system.typeinitializationexception?view=netframework-4.7.2). – Nicholas Pipitone Sep 28 '18 at 21:59
  • AppSettings indexer won't throw on a missing key but rather return null, so the first two assignments should be fine. The third and fourth however will throw an exception if the AppSettings keys are missing, blank or non-numeric (since int.Parse will throw an exception under those circumstances). Otherwise as you said, it's in the createTagDictionary – pinkfloydx33 Sep 29 '18 at 09:08