✅ need help with an error
hello! i'm very much a c# beginner making a text-based adventure game but i've come across an error i have no idea how to fix - here it is and here is a snippet of my code, but let me know if you need any more screenshots, much thanks 🙏
the error is this:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
player1 was null.
16 Replies
player1
has no value, its null
so when you try to access the attackDice
variable on it, it crashesis player1 not being given a value with 'private Player player1;' near the top of the screenshot? sorry i am hella new
i also have a class for the player so i thought that that would give player1 a value but i am not sure 🤔
no, thats you declaring the variable
it means the variable now exists, but it has the default value:
null
ohhhh right
you probahly want to write
private Player player1 = new Player();
then these errors appear 🤔
Sadly I cant see your screen, so you will need to show me the code 🙂
ahhh sorry about that haha that completely slipped my mind 😭 i'll send it now
you renamed your player variable
it was called
player1
before, now its player
oh yeah whoops 😭
if you want to rename things, use the rename tool
F2 by default, I think
it renames it everywhere, by following the code references
very smart
thank you very much, that's very useful 😭🙏
i got another unrelated error after that but i've managed to fix it - so otherwise my main problem has been fixed i think. thank you very much for your help!!! :)
you're welcome. You can mark this thread as resolved with
/close
thanks
!