Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I have a static class and lots of other classes.
Static Class has lots of variables and one of them is double:
C#
public static double speed = 2;

I try to change its value at the start of the program or at a little further:
C#
Data.speed = 2;

and it throws the next exception:
Quote:
An unhandled exception of type 'System.TypeInitializationException' occurred in Game X-O.exe

Additional information: The type initializer for 'Game_X_O.Data' threw an exception.


But when i change its value somewhere else, it works fine.
It also works fine if i create a new Windows Forms application and that is why i can't reproduce the problem.

I can also record a video of my Visual Studio to show the problem if needed.
If i can share some information to help, let me know.

Static Class:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Game_X_O
{
    static class Data
    {
        public static double speed = 2;

        static float plus = 1;
        static int percent = 20;
        static bool _in = true;

        public static Color item0_color = Color.White;
        public static float item0_plus = plus;
        public static bool item0_in = _in;
        public static int item0_percent = percent;

        public static Color item1_color = Color.Yellow;
        public static float item1_plus = plus;
        public static bool item1_in = _in;
        public static int item1_percent = percent;

        public static Color item2_color = Color.Red;
        public static float item2_plus = plus;
        public static bool item2_in = _in;
        public static int item2_percent = percent;

        public static Color lines_color = Color.White;
        public static int lines_percent = percent;
        public static double lines_speed = 2D;

        public static Color line_color = Color.White;
        public static int line_percent = percent;
        public static int line_plus = 20;

        public static int i1_size = 0;
        public static int i2_size = Calculate.arr_size * Calculate.arr_size + 3;
        public static int i3_size = i2_size + Calculate.arr_size * Calculate.arr_size + 1;
        public static int i4_size = i3_size + Form1.form1.items.itemslength;

        public static int i1_start = 0;
        public static int i1_stop = i1_start;

        public static int i2_start = 3 + Calculate.arr_size * Calculate.arr_size;
        public static int i2_stop = i2_start;

        public static int i3_start = 3 + Calculate.arr_size * Calculate.arr_size * 2;
        public static int i3_stop = i3_start;

        public static int i4_start;
        public static int i4_stop;
    }
}


Form1 Class where i call it:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Game_X_O
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Data.speed = 5; // exception

            InitializeComponent();

            FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;

            this.DoubleBuffered = true;

            form1 = this;

            w = System.Windows.Forms.Screen.GetBounds(this).Width;
            h = System.Windows.Forms.Screen.GetBounds(this).Height;

            New();
        }

        public static Form1 form1;

        public static int w;
        public static int h;
        public static int size = 3 + Calculate.arr_size * Calculate.arr_size * 2 + New_Items.max_itemslength + 1;

        public void New()
        {
            di = new Draw_Items(form1);
            game = new Game(form1);
            rects = new Rectangles(form1);
            items = new New_Items(form1);
            calc = new Calculate(form1);
            ls = new Lines(form1);
            ai = new Add_Items(form1);
            draw = new Draw(form1);
            ll = new Light_Lines(form1);
            mga = new Move_Game_Area(form1);

            Start();

            ci = new Control_Items(form1);
        }

        public Game game;
        public Rectangles rects;
        public New_Items items;
        public Calculate calc;
        public Lines ls;
        public bool single = false;
        public Draw_Items di;
        public Add_Items ai;
        public Draw draw;
        public Light_Lines ll;
        public Move_Game_Area mga;
        public Control_Items ci;

        private void Start()
        {
            game.Play();
        }
    }
}


InnerException:
Quote:
System.NullReferenceException: Object reference not set to an instance of an object.
at Game_X_O.Data..cctor() in c:\Users\Ziya1995\Desktop\Game X-O 18 - Copy\Game X-O\Data.cs:line 42
Posted
Updated 2-Mar-15 5:39am
v8
Comments
Sergey Alexandrovich Kryukov 2-Mar-15 10:16am    
What's the use of showing exception message without showing a line where it was thrown?
Why video? If you need help, you need appropriate issue report, comprehensive exception information, code sample...
—SA
Ziya1995 2-Mar-15 10:52am    
I added:
Data.speed = 5; // exception
Let me know, if i can help.
Sergey Alexandrovich Kryukov 2-Mar-15 11:20am    
It could be null exception id Data is null. Check it up under the debugger.
—SA
Ziya1995 2-Mar-15 11:28am    
I never do something to Data, i just named a static class "Data" and that is all.
I always used Data static variables and now can use, but in this case with this variable it gives exception.

So, what does "Data is null" mean and how can i fix it?
Sergey Alexandrovich Kryukov 2-Mar-15 11:37am    
If you don't know what null means, you should stop doing what you are doing and start learning software from the very, very beginning. You need to understand pointers and references, at least the idea. This is not a topic for Quick Questions & Answers. Not knowing it is the same as not knowing what is variable, constant, function, member and so on. Writing a single line of code would already be a waste of time until you at least read about the basic topics.
—SA

1 solution

OK, lets go through this. A Static Initializer failure is bodybag material. The type is then useless for the life of the process. It's caused by the static constructor throwing an exception. If you set the debugger to break on first chance exceptions, that'll show you the line.

My bet is it's this:
public static int i4_size = i3_size + Form1.form1.items.itemslength;

Which whilst being inline is set implicitly in the constructor. Static constructors get called the first time the type is 'mentioned' in the execution of code. Here you have one thing with unclear lifetime referencing another thing with unclear lifetime.

Can you not use normal instances instead, such that you can be sure they are instantiated in the correct order?
 
Share this answer
 
Comments
Thomas Daniels 2-Mar-15 12:09pm    
+5
Ziya1995 2-Mar-15 12:25pm    
+5
I removed my exception line to make the program work normally again.
Then i created, but not initialized:
public static Form1 form2;
and assigned:
public static int i4_size = i3_size + Form1.form2.items.itemslength;
That gave the same exception in another part of the code, but InnerException showed the same line of Data.

And as i understand when i call Data.speed, a simple initialized double variable, it initializes all the variables in the static Data class, when form1 is not initialized i don't know exactly this part, but i am gonna examine all that, thank you!

As i understand it is safer to use class instances instead of static classes to be sure they are instantiated normally.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900