【发布时间】:2011-04-10 13:51:33
【问题描述】:
首先是我的代码:
我已经评论了问题行
protected void Page_Load(object sender, EventArgs e)
{
StreamReader reader = new StreamReader(Request.PhysicalApplicationPath + "/directory.txt");
int i = 0;
int c = 0;
int d = 0;
List<string> alst = new List<string>();
List<string> nlst = new List<string>();
TableRow[] row = new TableRow[100];
TableCell[] cella = new TableCell[100];
TableCell[] cellb = new TableCell[100];
while (reader.Peek() > 0)
{
alst.Add(reader.ReadLine());
nlst.Add(reader.ReadLine());
d++;
}
foreach (string line in nlst)
{
if (i < d + 1)
{
cella[i].Text = nlst[i]; //this line
cellb[i].Text = alst[i]; //and this line always return a null return a null reference when ran
i++;
}
}
do
{
row[c].Cells.Add(cella[c]);
row[c].Cells.Add(cellb[c]);
c++;
} while (c != cella.Count());
foreach (TableRow item in row)
{
Table1.Rows.Add(item);
}
}
我检查过,所有涉及的变量都不为空。我试过清洗溶液。我也尝试将静态值放入 for i(如 0),仍然没有。
我已经盯着这个东西看了至少 2 个小时,改变了循环、ifs 和其他东西,但仍然无法弄清楚。
提前致谢, 亚当
【问题讨论】:
-
附带说明,您最好使用
using块来正确处理StreamReader,否则存在文件锁定甚至内存泄漏的风险。
标签: c# asp.net nullreferenceexception