System.NullReferenceException
Object reference not set to an instance of an object.
Where do I get this error you ask? In the Initialize method in the main Game1.cs class.
That’s where I’m writing
kattey.Initialize(breathe);
Thats the location of the error.
Let me show you the whole code, it’s not much. I just started this project. This is Game1.cs:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace Game6
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D tileTexture;
breathe breathe;
Katteyoo kattey;
Vector2 virtualScreen = new Vector2(1920, 1080);
Vector3 scalingFactor;
Matrix Scale;
Vector2 tilePosition = new Vector2(-128, 952);
List<tile> Tiles;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
Tiles = new List<tile>();
kattey.Initialize(breathe);
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D katteyooBreathing = Content.Load<Texture2D>("Katteyoo/katteyobigger");
breathe = new breathe(katteyooBreathing, 1, 2);
tileTexture = Content.Load<Texture2D>("Tiles/tile");
}
protected override void UnloadContent()
{
}
//other stuff
public void addTile()
{
tile Tile = new tile();
tilePosition.X += 128;
Tile.Initialize(tileTexture, tilePosition);
Tiles.Add(Tile);
}
//
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
//ground
if (Tiles.Count < 8)
{
addTile();
}
//
//tiles
for (var i = 0; i < Tiles.Count; i++)
{
Tiles[i].Update(gameTime);
}
//
// resolution scaling
float widthScale = (float)GraphicsDevice.PresentationParameters.BackBufferWidth / virtualScreen.X;
float heightScale = (float)GraphicsDevice.PresentationParameters.BackBufferHeight / virtualScreen.Y;
scalingFactor = new Vector3(widthScale, heightScale, 1);
Scale = Matrix.CreateScale(scalingFactor);
//
//katteyoo
kattey.Update(gameTime);
//
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, Scale);
//tiles
for (var i = 0; i < Tiles.Count; i++)
{
Tiles[i].Draw(spriteBatch);
}
//
breathe.Draw(spriteBatch, Vector2.Zero);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
Breathe.cs is the animation class for the character’s breathing animation. Here’s Katteyoo.cs(very small):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Game6
{
class Katteyoo
{
breathe b;
public void Initialize(breathe breathe)
{
b = breathe;
}
public void Update(GameTime gameTime)
{
b.Update(gameTime);
}
public void Draw(SpriteBatch spriteBatch)
{
b.Draw(spriteBatch, Vector2.Zero);
}
}
}
I really don’t know what’s wrong here. Everything seems to be exactly the same as the setups in my other projects which do in fact work without errors.
and this is Breathe.cs just in case:
Edit: Forget all of that, I figured it out. Thanks anyways.