❔ ✅ 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
customers = null;
You set the list of customers to null
sorry
Then you're trying to access things from it
I had deleted that line and forgot it
without that line
it still produces the same error
ill add a screenshot for greater context
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();
}
And where do you call this method?
I have rewritten this multiple times, is the issue that I need to initialize the list beforehand?
Also, $code
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/I am calling this method from a form Add button
ill post it
not '
The key under
Esc
That said, I don't see
SaveCustomers` being called anywhereim stupid asf
sorry
its been a long day here we go
Chances are, it is at this point that
customers
is null
so the error is referencing the point where its being called
I do initialize the list in this class
its initialized as null, but as a local variable
There's no local variable
customers
thoim sorry I dont think I understand
The field
customers
is null
So you pass null
to your method
That's where it comes fromso I should initialize it as an empty list and not a null list
For example, yes
ok thank you very much! I will try that
I must have gotten confused and thought null was the same as empty
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
etcwhen I swap the null customers for: List<Customer> customers = new List<Customer>();
when initializing it
I still get the same error
odd
hold on
Run the debugger ig
Place some breakpoints
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?
/close
if you want to close it
Or !close
, one of thosethere we are
cool beans!
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.