Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

C# - "Object reference not set to an instance of an object"

$
0
0
I am trying to create a simple GUI system, with buttons and text for now, but this little problem has me absolutely stumped. Basically, I'm creating a new instance of the class GUI and giving it a reference of the newly created window (wnd) which then stores it in the variable "w". With that, I loop through all the current buttons and draw it using the window's draw function (w.Draw()) but the variable "w" always ends up being null and giving me that error. Here's part of my code: // Program.cs static void Main(string[] args) { RenderWindow wnd = new RenderWindow(new VideoMode(800, 600), "Test"); Font font = new Font("ITCBLKAD.TTF"); GUI gui = new GUI(ref wnd); wnd.Closed += new EventHandler(CloseEvn); while (wnd.IsOpen) { wnd.DispatchEvents(); wnd.Clear(); gui.RenderAll(); wnd.Display(); } // ... // GUIHandler.cs class GUI { protected RenderWindow w; static protected List<Button> buttonContainer = new List<Button>(); public GUI() { } public GUI(ref RenderWindow window) { w = window; } public void RenderAll() { foreach (Button obj in buttonContainer) { obj.Draw(); } } } class Button : GUI { Text textObj; FloatRect rect; RectangleShape shape; Vector2f vec; State state; readonly string type = "button"; private Button(string text, Font _font, float xpos, float ypos, State _state) { vec.X = xpos; vec.Y = ypos; textObj = new Text(text, _font); textObj.Position = vec; state = _state; rect.Left = xpos; rect.Top = ypos; rect.Width = 10; rect.Height = 10; } public static Button NewText(string text, Font _font, float xpos, float ypos, State _state) { Button temp = new Button(text, _font, xpos, ypos, _state); buttonContainer.Add(temp); return temp; } public void Draw() { w.Draw(textObj); } } Is there anything I'm missing? I'll post the whole code if needed.

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>