Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this error when trying to write a .csv file back to my Lib folder. not sure how to do a Try Catch. Help please.

Thanks guys

error on line (system.NullReferenceExpetion)

foreach (Lunch s in lunchBindingSource.DataSource as List<Lunch>)



may code

<pre>private void btnWrite_Click(object sender, EventArgs e)
       {
           using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "csv|*.csv", ValidateNames = true })
           {
               if (sfd.ShowDialog() == DialogResult.OK)
               {
                   using (var sw = new StreamWriter(sfd.FileName))
                   {
                       var writer = new  CsvWriter(sw);
                       writer.WriteHeader(typeof(Lunch));

                       if (lunchBindingSource == null || lunchBindingSource.DataSource == null)
                       {
                           MessageBox.Show("Error in Saving.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                           return;
                       }


                       foreach (Lunch s in lunchBindingSource.DataSource as List<Lunch>)
                       {
                           writer.WriteRecord(s);
                       }
                   }
                   MessageBox.Show("Your Data has been Successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
           }
       }


What I have tried:

new List<String>();


List<String> names = new List<String>();
      names.Add("");
Posted
Updated 8-Feb-17 21:20pm
Comments
[no name] 7-Feb-17 18:53pm    
Okay so what is your question?
Member 12349103 7-Feb-17 19:19pm    
When i try to save a new .Csv and load i get the error.
[no name] 7-Feb-17 20:04pm    
Yes I can read. What is your question?
CHill60 7-Feb-17 19:11pm    
So lunchBindingSource has not been instantiated
cvogt61457 7-Feb-17 20:49pm    
Either lunchBindingSource has not been instantiated or it is not a List<lunch> object.

Hello again:
In the previous message i said that
surelly the problem  is luncBindingSource.DataSource can not be converted to List<Lunch> so it´s null


If you use:
List<Lunch> l=lunchBindingSource.DataSource as List<Lunch>;
if (l==null)
{
    MessageBox.Show("Error DataSource can not be converted to List.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
   return;
}
foreach (Lunch s in l)


you will see that the problem is that DataSource can not be converted to a List<lunch>.
So, you need to write code that converts that.
 
Share this answer
 
Comments
Member 12349103 9-Feb-17 19:43pm    
Thanks Guys...
Hello:
Inspecting your code it can be seen that nor luncBindingSource neither luncBindingSource.DataSource are null.

But in the foreaech you use:
C#
foreach (Lunch s in lunchBindingSource.DataSource as List<Lunch>)
that could be written:
C#
var l=lunchBindingSource.DataSource as List<Lunch>;
foreach (Lunch s in l)

the conversion of luncBindingSource.DataSource to a List<Lunch> surelly is the problem (that is luncBindingSource.DataSource can not be converted to List<Lunch> so it´s null).
 
Share this answer
 
Comments
Member 12349103 8-Feb-17 16:38pm    
Solution 1 almost did the trick but getting error on the l in this line.
System.NullReference
foreach (Lunch s in l)


I will add Write and Read code.

Thanks

private void btnWrite_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "csv|*.csv", ValidateNames = true })
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    using (var sw = new StreamWriter(sfd.FileName))
                    {
                        var writer = new CsvWriter(sw);
                        writer.WriteHeader(typeof(Lunch));
                        var l = lunchBindingSource.DataSource as List<Lunch>;
                        foreach (Lunch s in l)

                        {
                            writer.WriteRecord(s);
                        }
                    }
                    MessageBox.Show("Your Data has been Successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        

        private void btnRead_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    var sr = new StreamReader(new FileStream(ofd.FileName, FileMode.Open));
                    var csv = new CsvReader(sr);
                 //  lunchBindingSource.DataSource = csv.GetRecords<Lunch>().ToString();
                    lunchBindingSource.DataSource = csv.GetRecords<Lunch>(); /// use to create file
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lunchBindingSource.DataSource = new List<Lunch>();
        }
    }
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900