i find a problem
when i need to use content.load<>(“path”) to load my texture, if the path is like ./a/a, i dont know what i should write to replace “path”
if i write
content.load("./a/a")(without blank)
, it will say"System.NullReferenceException:“Object reference not set to an instance of an object.”"
if i write
content.load("./ a/ a")(with blank)
, it will say"Microsoft.Xna.Framework.Content.ContentLoadException:“The content file was not found.”"
what right way to write path
Try specifying a decreasing slash \ instead of /.
ok, i found i make a mistake
i use
list.add
to try to add textures, this cause error, but i dont know what
List player;
…
player.Add(content.Load(".\player\player2"));//error ,path is ok if player is Texture2D[]
i see the programme, it say \ is replaced to /, maybe the problem is not path
i have solve the problem by rewrite there.
Send the project here
this is the problem code example, replace Texture[] to List<> and initial can solve bug
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace MyGame;
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
//public List role = new List();
public Texture2D[] role;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override async void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
//role.Add(Content.Load<Texture2D>("player1"));
//role.Add(Content.Load<Texture2D>("player2"));
//role.Add(Content.Load<Texture2D>("player3"));
role[0] = Content.Load<Texture2D>("player1");
role[1] = Content.Load<Texture2D>("player2");
//error System.NullReferenceException:“Object reference not set to an instance of an object.”
role[2] = Content.Load(“player3”);
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
You have not initialised role.
you need to do:
List<Texture2D> role = new List<Texture2D>();
Then this should work:
role.Add(Content.Load<Texture2D>("player1"));
(When you get the error, it is role that is not set to an instance of an object)