Please someone tell me wtf im getting a nullreference here all of a sudden (pic inside)

So i just came back to this project after take a break from it for a while, i might have moved my folders around when i cleaned up my computer, could this mess up some connection between classes? Im pretty sure i didnt get this nullreference last time i worked on this game.

When debugging it should break at the exception and you can see what’s null. My money is on gameworld, but I can’t be entirely sure from just this code.

1 Like

The null refernece is at the charCreate variable where the line is pointed to, im just not sure why it throws a nullexception here, at the moment where im actually “turning” the variable into a new instance of a CreateCharacter button class? Its like its not even giving me the chance to create the variable before deeming it to be null?

You’re not turning the variable into an instance and you’re not creating the variable, that’s not how it works. You’re creating the instance and then assign it to the variable. The assignment can’t cause a NullReference, so it’s the creation that’s causing it. Check your other variables like gameWorld and Content.

Hmm it seems ive messed up my spritebatch and graphicsdevice somehow, oh well atleast im that much closer to the issue now :slight_smile:

If you store the gameworld.Content.Load into a variable:

Texture2D tex = gameworld.Content.Load<Texture2D>...

charCreate = new CreateCharacter(tex, graphicsdevice, etc.)
before calling you create new, this new line will likely be pointed as the faulty one instead of the createxyz one. As jjagg suggested.
Or then it may be within the constructor CreateCharacter there is a problem. Look at the stacktrace and inner exception if there are any

Well, your graphics device controller is called thisGraphics and not graphics. Trying to pass graphics.GraphicsDevice to your CreateCharacter() function will likely throw an error.

Edit: I see now that graphics is passed into LoadContent()… my bad.

I might have had a similar issue… and I think it’s related to referencing GraphicsDevice too early. See:

This basically says that you shouldn’t reference GraphicsDevice until LoadContent is called. In your code, it looks like you are referencing it before it’s completed i.e. you are calling base.LoadContent() after your load code (I am assuming… since I don’t see it in the lines above), so the framework hasn’t finished initializing the GraphicsDevice yet.

You could try moving the base.LoadContent() to the beginning of your LoadContent method, or you could also move the CreateCharacter() calls to the Initialize method (but also ensure you call base.Initialize() first, since Initialize essentially calls LoadContent).

The whole Initialize->LoadContent flow is a bit convoluted.

EDIT : I just realized that this is not the Game class, so it’s not clear when you are calling LoadContent on this class. So, I could be totally wrong here… but it doesn’t hurt to double check your LoadContent / Initialization sequences.