Search code examples
c#.netvisual-studiouser-interfaceterminal.gui

C# Cant find object reference when using terminal.gui


Ive been playing around with some server/client stuff and though id try using terminal.gui, and for some reason that i dont understand its acting up. i have 2 scripts; one to handle the backend stuff and one to handle the user interface/interraction.

script1:

using UI;

namespace ServerTest
{
    public class Server
    {
        static void Main() 
        {
            UI.UI.main();
            StartServ();
            RecieveConn.Start();
            MngSockets.Start();
        }
    }
}

script 2:

using ServerTest;
using Terminal.Gui;

namespace UI
{
    public class UI
    {
        static Window win = new Window(new Rect(0, 0, Application.Top.Frame.Width, Application.Top.Frame.Height), "MyApp");
        public static void main()
        {
            Application.Init();
            win.Add(new Label(0, 0, "asd"));

            Application.Run(win);
        }
    }
}

This gives me the error:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'UI.UI' threw an exception.
  Source=Server
  StackTrace:
   at UI.UI.main() in C:\Users\ThisPc\Desktop\ConsoleApp2\ConsoleApp2\ui.cs:line 29
   at ServerTest.Server.Main() in C:\Users\ThisPc\Desktop\ConsoleApp2\ConsoleApp2\Program.cs:line 23

Inner Exception 1:
NullReferenceException: Object reference not set to an instance of an object.

Though, if i do this it works:

using ServerTest;
using Terminal.Gui;

namespace UI
{
    public class UI
    {
        public static void main()
        {
            Window win = new Window(new Rect(0, 0, Application.Top.Frame.Width, Application.Top.Frame.Height), "MyApp");

            Application.Init();
            win.Add(new Label(0, 0, "asd"));

            Application.Run(win);
        }
    }
}

but then i cant use the win variable in other functions... Thanks in advance =)


Solution

  • Thx mjwills for the answer...

    just do

    using ServerTest;
    using Terminal.Gui;
    
    namespace UI
    {
        public class UI
        {
            static Window win = null;
            public static void main()
            {
                win = new Window(new Rect(0, 0, Application.Top.Frame.Width, Application.Top.Frame.Height), "MyApp");
    
                Application.Init();
                win.Add(new Label(0, 0, "asd"));
    
                Application.Run(win);
            }
        }
    }