Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I fix this System.StackOverFlow exception in my Pong code?
    primarykey
    data
    text
    <p>I am trying to make Pong in XNA/C# using a class for the Paddle and Ball</p> <p>Game1.cs:</p> <pre class="lang-cpp prettyprint-override"><code>using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace Pong { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Paddle Paddle1 = new Paddle(); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); } protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); Paddle1.Draw(); base.Draw(gameTime); } } } </code></pre> <p>Paddle.cs:</p> <pre class="lang-cpp prettyprint-override"><code>namespace Pong { class Paddle { SpriteBatch spriteBatch; ContentManager Content; Texture2D paddle1; Texture2D paddle2; Vector2 Paddle1; Vector2 Paddle2; public void LoadContent() { paddle1 = Content.Load&lt;Texture2D&gt;("pongpaddle1"); Paddle1 = new Vector2(); Paddle1.X = 50; Paddle1.Y = 50; } public void Draw() { spriteBatch.Begin(); //Causes NullReferenceException was unhandled, Object reference not set to an instance of an object. spriteBatch.Draw(paddle1, Paddle1, Color.White); spriteBatch.End(); } } } </code></pre> <p>I don't have anything in the Ball class yet, but it will use similar methods to Paddle.cs</p> <p>Every time I've ran the code, I keep getting a System.StackOverFlow exception whenever it hits this line of code in Game1.cs:</p> <pre class="lang-cpp prettyprint-override"><code>Paddle Paddle1 = new Paddle(); </code></pre> <p>How do I fix this? I don't see how it's run out of memory already.</p> <p>EDIT: Updated code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload