0

My problem is that i keep getting an error like this:

The type initializer for 'myproject.GV' threw an exception."

GV there is my class. The code looks fine, no errors when running but after I call on the conn with MySqlConnection the problem will appear. Example code I try to call conn is this : GV.conn.Open();

Before I put following code to my program it worked fine. Now it keep throwing errors:

public static class GV
{
    public static MySqlConnection conn = new MySqlConnection(
                "server=localhost;port=3306;uid=root;database=sysdb_elearn;password=***");
    public static MySqlCommand com = null;
    public static MySqlDataReader dr = GV.com.ExecuteReader();
    public static frmLearnerPanel flp = new frmLearnerPanel();
    public static frmLogIn frmLogin = new frmLogIn();
}
Nick
  • 831
  • 2
  • 9
  • 30
Terces Eman
  • 21
  • 1
  • 2
  • 2
    this in general seems like an extremely dangerous way to do database CRUD – Kritner Jul 19 '16 at 15:58
  • Probably something going wrong with your DB connection. Try to get details about the issue (see http://stackoverflow.com/a/4398380/870604). Also, you generally don't want a singleton on your DB connection, but that is another story. – ken2k Jul 19 '16 at 15:58
  • It looks like you've either left out a lot of code, or your class is incomplete. You want to be able to use this as a 'constants' class, right? If so, just put the connection string in conn, and instantiate a MySqlConnection in Main(). Also, I'm pretty sure you can't just use the dr member like that. – Jesse Smith Jul 19 '16 at 15:59
  • lol I'm so new to this coding technique, I just found out that I can make variables global so I just call it again and not creating another MySqlConnection conn = New MySqlConnection ....... on every forms. I tried the above code and it worked on the first try after further coding I lost track of my code and it won't work anymore – Terces Eman Jul 19 '16 at 16:05
  • `GV.com.ExecuteReader()` is your (main) problem here. com is null. So your constructor will never allow construction of your object. Avoid static variables wherever possible, unless absolutely necessary. You shouldn't have a static db connection. – ManoDestra Jul 19 '16 at 16:08
  • thanks guys for your responds. it helped me. what a shame on me lol I just removed the datareader like ManoDestra is pointing. then just declared it on my forms. It worked as before. thank you guys! – Terces Eman Jul 19 '16 at 16:24

1 Answers1

0

Immediate error that causes this behavior - NullReferenceException in static field inizialization on MySqlDataReader dr = GV.com.ExecuteReader(); (com is null at the time fields are initialized). When there is any exception during static properties initialization whole class marked as failed initialization and any future access to it will throw exception you observe. More info can be found in MSDN article TypeInitializationException.

Fix:

  • if you really need this data in static fields - wrap code in static method which handle exceptions and never throws. Note that caching reader in static variable is very non-practical as you can only read data from it once.

    public static MySqlDataReader dr = StaticInitializerThatDoesNotThrow();
    
  • better solution - refactor code to call SQL as necessary and properly dispose connection object. I.e. see for proper call - How to retrieve data from a SQL Server database in C#?.

Community
  • 1
  • 1
Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
  • lol fixed it thanks!! I remember before I didn't put MySqlDataReader on public static I thought it should work so I experiment a bit. I removed the datareader then just declare it on my forms. – Terces Eman Jul 19 '16 at 16:22