C# 空引用异常和 StreamReader
C# Null reference exception and StreamReader
我在从我的 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 returns 为空字符串.当然,尝试读取空字符串的索引零处的 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;
}
}
我在从我的 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 returns 为空字符串.当然,尝试读取空字符串的索引零处的 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;
}
}