"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
Angius4mo ago
$debug
MODiX
MODiX4mo 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
Angius4mo ago
Seems like mailbox might be null Or indexth item of it
slowly losing it
slowly losing itOP4mo ago
its not tho in the constructor, i make it not null even VSCode says that it's not null there
Angius
Angius4mo ago
What does the debugger say?
slowly losing it
slowly losing itOP4mo ago
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
Angius4mo ago
No description
slowly losing it
slowly losing itOP4mo ago
No description
slowly losing it
slowly losing itOP4mo ago
this is for VS i dont use VS
Angius
Angius4mo ago
VS Code also has some sort of a debugger So use that
slowly losing it
slowly losing itOP4mo ago
its giving me literally the exact same error as in console
Angius
Angius4mo ago
Place a breakpoint See what the values are Step through the code to see why they are what they are
SG97
SG974mo ago
aka debug
slowly losing it
slowly losing itOP4mo ago
No description
slowly losing it
slowly losing itOP4mo ago
the fuck is this
Angius
Angius4mo ago
You not running the debugger is what it is
slowly losing it
slowly losing itOP4mo ago
i just ran it and it gave me a richer text version of the error message in console
Angius
Angius4mo 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
Angius4mo 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
slowly losing it
slowly losing itOP4mo ago
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
Angius4mo 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
slowly losing it
slowly losing itOP4mo ago
explaining why im too stupid to use a debugger is not helping me fix my problem why is there a parameterless constructor
Angius
Angius4mo ago
Probably a quirk of structs
slowly losing it
slowly losing itOP4mo ago
can i override it? or is it stuck like that?
Angius
Angius4mo ago
Not sure if it even is the case I don't really use structs all that often Let alone such humongous ones
Jimmacle
Jimmacle4mo 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 Wise4mo 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

Did you find this page helpful?