写入.csv文件时为空引用 [英] Null reference when writing to .csv file

查看:171
本文介绍了写入.csv文件时为空引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将.csv文件写回我的Lib文件夹时出现此错误。不知道怎么做Try Catch。请帮忙。



谢谢大家



错误在线(system.NullReferenceExpetion)



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("");

推荐答案

您好:

检查您的代码可以看出, luncBindingSource 也没有 luncBindingSource.DataSource 为空。



但是在预测中你使用:

Hello:
Inspecting your code it can be seen that nor luncBindingSource neither luncBindingSource.DataSource are null.

But in the foreaech you use:
foreach (Lunch s in lunchBindingSource.DataSource as List<Lunch>)

可以写成:

that could be written:

var l=lunchBindingSource.DataSource as List<Lunch>;
foreach (Lunch s in l)



luncBindingSource.DataSource 转换为列表< Lunch> 肯定是问题(即 luncBindingSource.DataSource 无法转换为列表< Lunch> 所以它为null)。


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).


再次问好:

在上一条消息中我说
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)





你会发现问题是DataSource不能转换为List< lunch>。

所以,你需要编写转换它的代码。



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.


这篇关于写入.csv文件时为空引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆