rds1983 / Myra

UI Library for MonoGame, FNA and Stride
MIT License
742 stars 93 forks source link

System.NullReferenceException: 'Object reference not set to an instance of an object.' #456

Closed Kubrix closed 7 months ago

Kubrix commented 7 months ago
using Myra;
using Myra.Graphics2D.UI;
public class Game1 :Game
{
  private Desktop _desktop;
  Grid grid;
}
protected override void LoadContent()
{
MyraEnvironment.Game = this;

var grid = new Grid
{
  RowSpacing = 8,
  ColumnSpacing = 8
};

_desktop = new Desktop();
_desktop.Root = grid;
}
protected override void Draw(GameTime gameTime)
{
...
var panel = new Panel();
var positionedText = new Label();
positionedText.Text = "Positioned Text";
positionedText.Left = 50;
positionedText.Top = 100;
panel.Widgets.Add(positionedText);
grid.Widgets.Add(panel);
..
_desktop.Render();
base.Draw(gameTime);
break;

Exception on grid.Widgets.Add(panel); grid was null.

rds1983 commented 7 months ago

Move the ui creation code into LoadContent. You don't want it to be recreated every frame.

Kubrix commented 7 months ago

Thanks!