C
C#2y ago
bernie2024

❔ ✅ Struggling with function showing error, "Object reference not set to an instance of an object"

have been trying to solve this issue, not sure what the problem is. public static class CustomerDB { private const string dir = @"C:\C#\Files"; private const string path = dir + "Customers.txt"; public static void SaveCustomers(List<Customer> customers) { // create the output stream for a text file that exists StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); // write each customer foreach (Customer customer in customers) { textOut.Write(customer.FirstName + "|"); textOut.Write(customer.LastName + "|"); textOut.WriteLine(customer.Email); } // write the end of the document textOut.Close(); } I get the error "Exception Thrown System.NullReferenceException: 'Object reference not set to an instance of an object. customers was null." If any additional reference code is needed for context I can provide it, the point of this is to add a "Customer" to a database of customers What am I doing wrong here? it only has the issue once reaching the 'Foreach (Customer customer in customers)' statement.
32 Replies
Angius
Angius2y ago
customers = null; You set the list of customers to null
bernie2024
bernie2024OP2y ago
sorry
Angius
Angius2y ago
Then you're trying to access things from it
bernie2024
bernie2024OP2y ago
I had deleted that line and forgot it without that line it still produces the same error ill add a screenshot for greater context
bernie2024
bernie2024OP2y ago
bernie2024
bernie2024OP2y ago
public static void SaveCustomers(List<Customer> customers) { // create the output stream for a text file that exists StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); // write each customer foreach (Customer customer in customers) { if (customer == null) { throw new ArgumentNullException(nameof(customer), "Customer object cannot be null"); } textOut.Write(customer.FirstName + "|"); textOut.Write(customer.LastName + "|"); textOut.WriteLine(customer.Email); } // write the end of the document textOut.Close(); }
Angius
Angius2y ago
And where do you call this method?
bernie2024
bernie2024OP2y ago
I have rewritten this multiple times, is the issue that I need to initialize the list beforehand?
Angius
Angius2y ago
Also, $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
bernie2024
bernie2024OP2y ago
I am calling this method from a form Add button ill post it
Angius
Angius2y ago
not ' The key under Esc That said, I don't see SaveCustomers` being called anywhere
bernie2024
bernie2024OP2y ago
im stupid asf sorry its been a long day here we go
private void btnAdd_Click(object sender, EventArgs e)
{
frmAddCustomer addCustomerForm = new frmAddCustomer();
Customer customer = addCustomerForm.GetNewCustomer();
if (customer != null)
{
CustomerDB.SaveCustomers(customers);
CustomerListBox();
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
frmAddCustomer addCustomerForm = new frmAddCustomer();
Customer customer = addCustomerForm.GetNewCustomer();
if (customer != null)
{
CustomerDB.SaveCustomers(customers);
CustomerListBox();
}
}
Angius
Angius2y ago
Chances are, it is at this point that customers is null
bernie2024
bernie2024OP2y ago
so the error is referencing the point where its being called I do initialize the list in this class
bernie2024
bernie2024OP2y ago
bernie2024
bernie2024OP2y ago
its initialized as null, but as a local variable
Angius
Angius2y ago
There's no local variable customers tho
bernie2024
bernie2024OP2y ago
im sorry I dont think I understand
Angius
Angius2y ago
The field customers is null So you pass null to your method That's where it comes from
bernie2024
bernie2024OP2y ago
so I should initialize it as an empty list and not a null list
Angius
Angius2y ago
For example, yes
bernie2024
bernie2024OP2y ago
ok thank you very much! I will try that I must have gotten confused and thought null was the same as empty
Angius
Angius2y ago
Could be If you're using .NET 6 or newer, be sure to enable nullable reference types They help you figure out errors of this kind For example, a List<int> cannot be null, you need to explicitly use List<int>? to allow for it being null etc
bernie2024
bernie2024OP2y ago
when I swap the null customers for: List<Customer> customers = new List<Customer>(); when initializing it I still get the same error
Angius
Angius2y ago
odd
bernie2024
bernie2024OP2y ago
hold on
Angius
Angius2y ago
Run the debugger ig Place some breakpoints
bernie2024
bernie2024OP2y ago
ok thank you friend It now doesnt throw that error I initialized it like that and removed a line setting it to null and now its working although not displaying which means a different, but new issue big step thanks again! I will continue debugging Do I delete post or mark as finished?
Angius
Angius2y ago
/close if you want to close it Or !close, one of those
bernie2024
bernie2024OP2y ago
there we are cool beans!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server