CosmosOS / Cosmos

Cosmos is an operating system "construction kit". Build your own OS using managed languages such as C#, VB.NET, and more!
https://www.goCosmos.org
BSD 3-Clause "New" or "Revised" License
2.94k stars 552 forks source link

NullReferenceException when i use a static color from an other class #3072

Closed Schaapie-D2 closed 3 months ago

Schaapie-D2 commented 3 months ago

Area of Cosmos - What area of Cosmos are we dealing with?

Code ¯_ (ツ)_/¯

Expected Behaviour - What do you think that should happen?

Get the specified color from a class

Actual Behaviour - What unexpectedly happens?

OS crashes and gives a NullReferenceException

Reproduction - How did you get this error to appear?

the colors:

using Cosmos.System.Graphics.Fonts;
using System.Drawing;

namespace HontelOS.Resources
{
    public class Style
    {
        #region General
        public static Color PrimaryColor = Color.FromArgb(60, 60, 255);
        public static Color SecondaryColor = Color.FromArgb(60, 100, 255);
        public static Color TritraryColor = Color.FromArgb(60, 40, 255);

        public static Font SystemFont = PCScreenFont.Default;
        #endregion

        #region System
        public static Color Dock_BackgroundColor = Color.FromArgb(194, 194, 194);

        public static Color TopBar_BackgroundColor = Color.FromArgb(194, 194, 194);
        #endregion

        #region GUI
        public static Color Window_BackgroundColor = Color.White;
        public static Color Window_HandelColor = Color.FromArgb(243, 243, 243);
        public static Color Window_HandelButtonGlowColor = Color.LightGray;

        public static Color Button_NormalColor = Color.FromArgb(60, 60, 255);
        public static Color Button_HoverColor = Color.FromArgb(60, 100, 255);
        public static Color Button_PressedColor = Color.FromArgb(60, 40, 255);
        public static Color Button_DisabledColor = Color.Gray;

        public static Color ContextMenu_BackgroundColor = Color.FromArgb(240, 240, 240);
        public static Color ContextMenu_HoverColor = Color.FromArgb(180, 180, 180);
        #endregion
    }
}

example of it being used:

public Color BackgroundColor = Style.Window_BackgroundColor;

Version - Were you using the User Kit or Dev Kit? And what User Kit version or Dev Kit commit (Cosmos, IL2CPU, X#)?

Devkit

9xbt commented 3 months ago

Cosmos is really funky with static stuff from my past experience, you should make the variables static but have like an Init() void that initializes them and call that void from BeforeRun()

Schaapie-D2 commented 3 months ago

I did that but it still gives that exception. It now gives it when the Init() method gets called. Maybe it doesn't like to interact with the variables?

Schaapie-D2 commented 3 months ago

Fixed it! code:

using Cosmos.System.Graphics.Fonts;
using System.Drawing;

namespace HontelOS.GUI
{
    public class Style
    {
        #region General
        public Color PrimaryColor;
        public Color SecondaryColor;
        public Color TritraryColor;

        public Font SystemFont;
        #endregion

        #region System
        public Color Dock_BackgroundColor;

        public Color TopBar_BackgroundColor;
        #endregion

        #region GUI
        public Color Window_BackgroundColor;
        public Color Window_HandleColor;
        public Color Window_HandleButtonGlowColor;

        public Color Button_NormalColor;
        public Color Button_HoverColor;
        public Color Button_PressedColor;
        public Color Button_DisabledColor;

        public Color ContextMenu_BackgroundColor;
        public Color ContextMenu_HoverColor;
        #endregion

        public Style()
        {
            PrimaryColor = Color.FromArgb(60, 60, 255);
            SecondaryColor = Color.FromArgb(60, 100, 255);
            TritraryColor = Color.FromArgb(60, 40, 255);

            SystemFont = PCScreenFont.Default;

            Dock_BackgroundColor = Color.FromArgb(194, 194, 194);

            TopBar_BackgroundColor = Color.FromArgb(194, 194, 194);

            Window_BackgroundColor = Color.White;
            Window_HandleColor = Color.FromArgb(243, 243, 243);
            Window_HandleButtonGlowColor = Color.LightGray;

            Button_NormalColor = Color.FromArgb(60, 60, 255);
            Button_HoverColor = Color.FromArgb(60, 100, 255);
            Button_PressedColor = Color.FromArgb(60, 40, 255);
            Button_DisabledColor = Color.Gray;

            ContextMenu_BackgroundColor = Color.FromArgb(240, 240, 240);
            ContextMenu_HoverColor = Color.FromArgb(180, 180, 180);
        }
    }
}

Kernel: add

public static Style style;

protected override void BeforeRun()
{
    style = new Style();
}