C# 空引用异常和 StreamReader

标签 c# windows-forms-designer

从我的 txt 文件中读取数据时出现空引用异常。

     public class Appointments : List<Appointment>
        {
            Appointment appointment;

            public Appointments()
            {

            }

            public bool Load(string fileName)
            {
                string appointmentData = string.Empty;
                using (StreamReader reader = new StreamReader(fileName))
                {
                    while((appointmentData = reader.ReadLine()) != null)
                    {
                        appointmentData = reader.ReadLine();
                        //**this is where null ref. exception is thrown** (line below)
                        if(appointmentData[0] == 'R')
                        {
                            appointment = new RecurringAppointment(appointmentData);
                        }
                        else
                        {
                            appointment = new Appointment(appointmentData);
                        }
                        this.Add(appointment);
                    }
                    return true;
                }
            }

RecurringAppointment 继承自 Appointments。文件存在,文件位置正确。有趣的是,该程序在 30 分钟前运行,我只是将 Load 方法从下面更改为您在上面看到的内容:

 public bool Load(string fileName)
        {
            string appointmentData = string.Empty;
            using (StreamReader reader = new StreamReader(fileName))
            {
                while((appointmentData = reader.ReadLine()) != null)
                {
                    appointmentData = reader.ReadLine();
                    if(appointmentData[0] == 'R')
                    {
                        this.Add(appointment = new RecurringAppointment(appointmentData));
                    }
                    else
                    {
                        this.Add(appointment = new Appointment(appointmentData));
                    }
                }
                return true;
            }
        }

现在这两种情况都不起作用。

最佳答案

您的代码在每个循环中读取两次。这意味着,如果当您读取文件的最后一行时文件的行数为奇数,则 while 语句中针对 null 的检查允许您的代码进入循环,但随后的 ReadLine 返回空字符串。当然,尝试读取空字符串的索引零处的 char 会抛出 NRE 异常。

你的文件也有空行的问题。如果有空行,再次读取索引零将抛出索引超出范围异常

你可以用这种方式修复你的代码

public bool Load(string fileName)
{
    string appointmentData = string.Empty;
    using (StreamReader reader = new StreamReader(fileName))
    {
        while((appointmentData = reader.ReadLine()) != null)
        {
            if(!string.IsNullOrWhiteSpace(appointmentData))
            {
                if(appointmentData[0] == 'R')
                    this.Add(appointment = new RecurringAppointment(appointmentData));
                else
                    this.Add(appointment = new Appointment(appointmentData));
            }
        }
        return true;
    }
}

关于C# 空引用异常和 StreamReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35676576/

相关文章:

c# - 为什么在这种情况下调用 Focus() 没有设置焦点?

c# - 正确渲染覆盖的 TreeView

c# - 销毁游戏对象后缺少引用的问题

c# - Dapper 和存储过程

c# - Windows 窗体 | MenuStrip 子项 (ToolStripMenuItem) 更改光标不起作用

c# - 绘制控件后如何在 Visual Studio 2010 RC Windows 窗体设计器中取回光标?

c# - 为什么用动态方法设置只读字段会导致此类错误?

c# - 我怎样才能从标签中减去一定数量

winforms - Windows FORms 字体在编译的应用程序中看起来呈锯齿状

winforms - 使用多个面板设计 Windows.Form -> 如何隐藏一个面板(如 PS 层)