Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

well I have googled it and got some answers, asked many times, answered many times. as I read viewport is the "inside window" So I have 2 integers, height and width and this code:

        heightofwindow = graphicsDevice.Viewport.Bounds.Height;
        widtthofwindow = graphicsDevice.Viewport.Bounds.Width;

However, I get the error. It doesnt matter if I put it under update or draw. This seems like a more of language problem due to my limited knowlodge.

Object reference not set to an instance of an object.
share|improve this question
What's the error? – William Mariager Aug 6 '12 at 0:23
"Object reference not set to an instance of an object." – Newell Aug 6 '12 at 0:27
graphicsDevice or Viewport or Bounds is null then – indeed005 Aug 6 '12 at 0:49
@Newell, How do you initialize "graphicsDevice"? – William Mariager Aug 6 '12 at 0:51
Well apperantly the incorrect way. It seems I've mistaken " GraphicsDeviceManager" for actual graphicsdevice. Can you tell me how can I init. it ? It seems it requires 3 parameters that I havent need until now. I think i can just init. last 2 class with no parameters but how do I init graphicsadapter ? – Newell Aug 6 '12 at 1:32
up vote 0 down vote accepted

The graphics device manager is created in the game class constructor...

You should use the reference that provides the game instance to access to the GraphicsDevice

if you are inheriting from Game class... in your main class code you can use


if you are inheriting from GameComponent... in your component code you can use

share|improve this answer
how do I init graphicsdevice ? I dont have/dont know how to init graphicsadapter. – Newell Aug 6 '12 at 1:33
@Newell, when you create a new XNA game project, it does this for you in the generated Game1.cs. Check the constructor of Game1. – Justin Skiles Aug 6 '12 at 2:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.