对明显非空对象的空引用
首先是我的代码:
我已经评论了我检查过的问题行
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);
}
}
,并且涉及的所有变量都不为空。我尝试过清洁溶液。我也尝试过为 i 添加静态值(如 0),但仍然没有任何结果。
我已经盯着这个东西至少2个小时了,改变了循环、ifs和其他东西,但仍然无法弄清楚。
提前致谢, 亚当
First here is my code:
I have commented the problem lines
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);
}
}
I have checked and all of the variables involved are not null. I have tried cleaning the solution. I have also tried putting static values in for i (like 0), still nothing.
I have been staring at this thing for at least 2 hours, changing loops, ifs, and other things and can still not figure it out.
Thanks in advance,
Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会创建一个数组,但不会初始化其值。因此
失败,因为
cella[i]
始终为null
并且.Text
不存在(同样适用于cellb[i])。
您必须首先初始化数组或在循环中生成新的 TableCell 对象
此外:
cellb[i] = new TableCell { Text = alst[i] };
对我来说看起来像是一个错误 -N
转到单元格A
并A
转到单元格B
?using
语句。这可以确保正确处理流 - 即使发生错误。This creates an Array but does not initialize its values. So
fails because
cella[i]
is alwaysnull
and.Text
does not exist (same applies forcellb[i]
).You will have to initialize your array first or generate a new TableCell object in your loop
Furthermore:
cellb[i] = new TableCell { Text = alst[i] };
looks like an error to me -N
goes to cellA
andA
goes to cellB
?using
statement when handling streams (and otherIDisposable
objects). This makes sure the stream is properly disposed - even when errors occur.当您声明
TableCell[] cella = new TableCell[100];
时,您将创建一个包含 100 个对TableCell
引用的数组,所有这些引用均为null
。如果您尝试执行cella[i].Text = nlst[i];
,cella[i]
为null
,因此您会得到当您尝试分配null.Text
时出现异常。听起来您需要一个循环来填充
cella
和cellb
的所有元素的值。When you declare
TableCell[] cella = new TableCell[100];
you are creating an array of 100 references toTableCell
, all of which arenull
. If you try to executecella[i].Text = nlst[i];
,cella[i]
isnull
, so you get an exception when you try to assignnull.Text
.It sounds like you need a loop to fill in values for all the elements of
cella
andcellb
.您永远不会实例化该数组中的
TableCell
对象;您只是实例化数组本身。您需要为每个条目创建new TableCell()
对象,然后才能使用它们的属性。You are never instantiating the
TableCell
objects in that array; you are only instantiating the array itself. You need to createnew TableCell()
objects for each entry before you can use their properties.