String asset name problem - help

So i’m learning to do 2D games with a book and i have some problem here

public class TexturedPrimitive
{
protected Texture2D mImage;
protected Vector2 mPosition;
protected Vector2 mSize;

    public TexturedPrimitive(string imageName, Vector2 position, Vector2 size)
    {
        mImage = Game1.sContent.Load<Texture2D>(imageName);
        
        mPosition = position;
        mSize = size;

    }

the problem is with imageName:Object reference not set to an instance of an object.

Look at where you are calling the constructor. You will be passing a null value for imageName.

imageName value is “UWB-JPG”

imageName value is “UWB-JPG”

The error message you gave said that imageName was null, so double check what you are sending it when constructing the TexturePrimitive object. Can you provide a snippet of the code you use that constructs the TexturePrimitive object?

Are you passing the path of the file to be loaded, and not just the name of it?

What is “sContent”??? shouldn’t this be just Game1.Content.Load?

It is just a typo mistake, or else the compiler would have marked this as an error while building: "Error 13 ‘xyz’ does not contain a definition for ‘abcd’ and no extension method ‘abcd’ accepting a first argument of type…
whereas Object reference is a runtime error.
It is likely imageName is null, or Game1 as we don’t see how this one is initialized.

I’m guessing that imageName is null, but the OP said it has a value, so I was wondering if sContent was a typo or it could actually be an object (maybe a “static field” on Game1 that is null since Game1 is an actual class name (in a new project) and not an instance of the class and doesn’t contain “Content”. My theory is he’s created a static field to contain the “content” so he doesn’t have to store a reference to the class or pass it around.

but since he mentioned:

imageName:Object reference not set to an instance of an object.

Usually the incriminated line is pointed, not the variable. And has visual places the popup almost at the end…
“Additionnal Information: Object reference not set to an instance of an object.”

Until we get the chance to know the whole stacktrace and the watches, we can only do assumption :confused:

Without any further information, all we can suggest is to use the debugger, find out which variable is null and follow it back up the callstack to see where it is coming from.

Harag what you said is right,i did created a static field as you all can see here:
(part of the Game1 class):

static public SpriteBatch sSpriteBatch;
static public GraphicsDeviceManager sGraphics;
static public ContentManager sContent;
const int kNumObjects = 4;
//TP Class
TexturedPrimitive[] mGraphicsObject;
int mCurrentIndex = 0;

I tried to load the path of the file directly (replacing the imageName with the path),but that wasn’t good either

After checking few of your assumptions,sContent is null

Looks like sContent is your problem, you need to populate that with “this.Content” when you first run the game.

public Initialize()
{
Game1.sContent = this.Content;
}

Don’t forget to populate the other statics too.

going to try right now,will tell you the outcome,thank you very much

Out of curiosity which book are you learning from?

I found the tutorials on this site very good:

http://gameprogrammingadventures.org

The Monogame tutorial is the latest one (and a weekly series), but he’s done some older ones in XNA - which you could still do but would need to tweak as you go along - e.g. the content pipeline is quite different to how it was in XNA, but the code should be basically the same.

Couple more links in case you’ve not found them, which I found really good.

http://www.riemers.net/
http://rbwhitaker.wikidot.com/start

and one for OOP patterns:

http://gameprogrammingpatterns.com/


:slight_smile:

actually i found the sites you wrote,i told myself that i would finish learning from the book and then go from sit to site to collect knowledge

And the path you provided had the usual back-slashes and periods…?

And your main content folder name corresponds to the folder you’ve been dropping files into…?