C
C#3mo ago
Gamer1234556

✅ Library is a null value

Hello, I am currently making a library application using Winforms. However, I am currently facing the issue of getting a null value for my library whenever I try to add a book. Can someone tell me what the issue with my code is? Github: https://github.com/Allen620-arc/LibraryManagementSystem
GitHub
GitHub - Allen620-arc/LibraryManagementSystem
Contribute to Allen620-arc/LibraryManagementSystem development by creating an account on GitHub.
16 Replies
Angius
Angius3mo ago
That's a lot of files and a lot of lines of code Where does the error happen?
Gamer1234556
Gamer12345563mo ago
Well there are a lot of places where that error happens For example: When pressing Add Books button, filling out the form and pressing save, I get an error at line 26 saying: System.NullReferenceException: 'Object reference not set to an instance of an object.' library was null. This is in the AddBooks.cs file btw
Angius
Angius3mo ago
There's no such file in the repo Did you mean AddBookForm.cs?
Gamer1234556
Gamer12345563mo ago
Yeah sorry
Angius
Angius3mo ago
Well, that would mean that either the constructor is not called, or the constructor itself gets a null passed to it It seems to be called from LibraryForm And I don't see the library field being initialized anywhere in there So, that's your null
Gamer1234556
Gamer12345563mo ago
You mean in the LibraryForm? I initialized it in line 15 there
Angius
Angius3mo ago
No, you did not
Angius
Angius3mo ago
No description
Angius
Angius3mo ago
You created a field library of type Library and left it uninitialized
MODiX
MODiX3mo ago
Angius
REPL Result: Failure
class Bar
{
public string Name => nameof(Bar);
}
class Foo
{
private Bar _bar;

public void Check()
=> Console.WriteLine($"Bar {(_bar is null ? "is" : "is not")} null");

public void SayIt()
=> Console.WriteLine($"Bar is named {_bar.Name}");
}

var f = new Foo();
f.Check();
f.SayIt();
class Bar
{
public string Name => nameof(Bar);
}
class Foo
{
private Bar _bar;

public void Check()
=> Console.WriteLine($"Bar {(_bar is null ? "is" : "is not")} null");

public void SayIt()
=> Console.WriteLine($"Bar is named {_bar.Name}");
}

var f = new Foo();
f.Check();
f.SayIt();
Console Output
Bar is null
Bar is null
Exception: NullReferenceException
- Object reference not set to an instance of an object.
- Object reference not set to an instance of an object.
Compile: 536.248ms | Execution: 44.995ms | React with ❌ to remove this embed.
Angius
Angius3mo ago
You said "this is a box for apples" and did not put any apples in it
Gamer1234556
Gamer12345563mo ago
Ok yeah I fixed the error Thanks I didn't know how I missed that How do I close post again?
Angius
Angius3mo ago
$close
MODiX
MODiX3mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Gamer1234556
Gamer12345563mo ago
$close
MODiX
MODiX3mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server