C
C#2y ago
d_aydream

✅ 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
Pobiega
Pobiega2y ago
player1 has no value, its null so when you try to access the attackDice variable on it, it crashes
d_aydream
d_aydreamOP2y ago
is 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 🤔
Pobiega
Pobiega2y ago
no, thats you declaring the variable it means the variable now exists, but it has the default value: null
d_aydream
d_aydreamOP2y ago
ohhhh right
Pobiega
Pobiega2y ago
you probahly want to write private Player player1 = new Player();
d_aydream
d_aydreamOP2y ago
d_aydream
d_aydreamOP2y ago
then these errors appear 🤔
Pobiega
Pobiega2y ago
Sadly I cant see your screen, so you will need to show me the code 🙂
d_aydream
d_aydreamOP2y ago
ahhh sorry about that haha that completely slipped my mind 😭 i'll send it now
d_aydream
d_aydreamOP2y ago
Pobiega
Pobiega2y ago
you renamed your player variable it was called player1before, now its player
d_aydream
d_aydreamOP2y ago
oh yeah whoops 😭
Pobiega
Pobiega2y ago
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
d_aydream
d_aydreamOP2y ago
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!!! :)
Pobiega
Pobiega2y ago
you're welcome. You can mark this thread as resolved with /close
d_aydream
d_aydreamOP2y ago
thanks !
Want results from more Discord servers?
Add your server