Problems obtaining the GraphicsDevice in a component

Everytime I try to write something I get hung up on the same problem. I can't get access to my GraphicsDevice so that I'd be able to draw things through components. I've tried several methods listed, such as these:

graphics = Game.GameServices.GetService<IGraphicsDeviceService>();
(Where graphics is of type IGraphicsDeviceService) I then call a graphics.GraphicsDevice, but with no success.

Another way I've tried to do things is getting to the GraphicsDevice directly through a public property in my component. The obvious thing to do would be to pass the default "graphics" GraphicsComponent into the component, and then use the GraphicsDevice property to draw things in my code. No luck. IntelliSense and the IDE wasn't able to pick up that graphics and my component wanted to talk to each other, thus giving me no options for objects my component could reference.

One last thing that I tried to do was to get the GraphicsDevice through the Game property in my program. I'm not sure if this is allowed, or if it's supposed to work, but I tried it anyway. It went something like this inside the component:

GraphicsDevice graphics = ((GraphicsDevice)this.Game.GameComponents[0]).GraphicsDevice;

No luck. Each time I run any of these cases I get a runtime exception of type "NullReferenceException"

I'm not sure what's causing it, or if it's a bug or something, but it's really annoying and frustrating. Please help!


Answer this question

Problems obtaining the GraphicsDevice in a component

  • Tryst

    Yep, it's working now, but after a big headache. It's important to realize the order in which things are added to the list of GameComponents in the main Game object. Also, when trying to retrieve something from the list of game components, make sure that the object has been instantiated! Other than that, it's simple stuff.

  • Pradeep T

    Well, I'm a moron. I didn't think to check the order in which things were created- looking back I probably should have.

    So for reference, and so other XNA'ers won't experience the same pain: Don't reference things that haven't been instantiated yet!


  • Callavin

    A 'NullReferenceException' would suggest that you are attempting to grab the device instance before it has been created.

    A couple of questions:

    - Does your application (sans custom game components) start and render correctly (i.e. the buffer gets cleared)
    - At which point in your application/game component do you attempt to grab the graphics device reference
    - Have you made any modifications to the InitializeComponent() method of your Game-inherited class

    Make sure that you aren't trying to grab the GraphicsDevice reference in your GameComponent's constructor (the GraphicsComponent won't be in the GameComponents list at this stage). Do that in the Start() method or a LoadResources() method.

  • jsedlak

    just for the record, the way to access the graphics device from anywhere (assuming you know it's already been instantiated) is:

    GraphicsDevice graphicsDevice = ((GraphicsComponent)this.Game.GameComponents[0]).GraphicsDevice;

    I'm assuming that since the graphics component is always added to the collection first by default, that's why it's always in index 0.  I kept wondering why the code you had wouldn't compile, but it turns out it was casting to the wrong thing, lol

  • Deepu_a

    Bizzump!

    Can anyone verify this as a bug or an error


  • Problems obtaining the GraphicsDevice in a component