"Object reference not set to an instance of an object"

code: https://github.com/axololly/chess/tree/main/Board.cs#L1073 not sure why this is happening. i changed the Board from a class to a struct so it's no longer heap allocated (used in an engine, so these things matter) and now it's raising weird errors like this
GitHub
chess/Board.cs at main · axololly/chess
A chess move generator I made over the summer because my friends didn't want to go out with me. - axololly/chess
29 Replies
Angius
Angius3mo ago
$debug
MODiX
MODiX3mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Angius
Angius3mo ago
Seems like mailbox might be null Or indexth item of it
it’s raining outside
its not tho in the constructor, i make it not null even VSCode says that it's not null there
Angius
Angius3mo ago
What does the debugger say?
it’s raining outside
haven't used it worked on a python project instead lemme take a look
// Fill the board with pieces based on the FEN string
Mailbox = new Piece[64];
Array.Fill(Mailbox, Piece.Empty);
// Fill the board with pieces based on the FEN string
Mailbox = new Piece[64];
Array.Fill(Mailbox, Piece.Empty);
Angius
Angius3mo ago
No description
it’s raining outside
this is for VS i dont use VS
Angius
Angius3mo ago
VS Code also has some sort of a debugger So use that
it’s raining outside
its giving me literally the exact same error as in console
Angius
Angius3mo ago
Place a breakpoint See what the values are Step through the code to see why they are what they are
SG97
SG973mo ago
aka debug
it’s raining outside
the fuck is this
Angius
Angius3mo ago
You not running the debugger is what it is
it’s raining outside
i just ran it and it gave me a richer text version of the error message in console
Angius
Angius3mo ago
Place a breakpoint where the Maibox is set See if it gets hit Step through the code See if maybe this bit of code executes before mailbox gets set
Angius
Angius3mo ago
See if there's no off-by-one errors Etc See what index is on each iteration And what is at that index in the mailbox
it’s raining outside
debugger did jack shit turns out that its inventing a new base overload i have this:
public Board(string FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
public Board(string FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
but it thinks its this:
public Board() {}

public Board(string FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
public Board() {}

public Board(string FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
Angius
Angius3mo ago
Debugger does what you tell it to do. It doesn't magically fix your code, it lets you gain insight into your code as it runs That you don't know how to use it and refuse to learn is not the debugger's fault
it’s raining outside
explaining why im too stupid to use a debugger is not helping me fix my problem why is there a parameterless constructor
Angius
Angius3mo ago
Probably a quirk of structs
it’s raining outside
can i override it? or is it stuck like that?
Angius
Angius3mo ago
Not sure if it even is the case I don't really use structs all that often Let alone such humongous ones
Jimmacle
Jimmacle3mo ago
how much data is in board? i suspect it shouldn't be a struct in the first place and yes structs always have an implicit parameterless constructor yeah that's definitely too big to be a struct just make it a class, a struct isn't giving you any benefits here and it may actually be worse if you don't know why you're making something a struct just make it a class, that's a good rule of thumb
Austin Wise
Austin Wise3mo ago
Here is the reference on where the parameterless constructor came from: https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/structs.md#1549-constructors there are other behavior differences that can cause problems if you change an existing class to a struct. Like if someone passed your Board to a method that change some of the state, the caller would no longer see the changes after the method returns. So you might want to leave it as class for backwards compatibility, in addition to the reasons Jimmacle mentioned.
GitHub
csharpstandard/standard/structs.md at standard-v6 · dotnet/csharpst...
Working space for ECMA-TC49-TG2, the C# standard committee. - dotnet/csharpstandard
Want results from more Discord servers?
Add your server