[英]Null reference on an apparent not null object
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输入静态值(例如0),还是没有。
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. 我已经盯着这个东西至少两个小时了,改变了循环,ifs和其他东西,但仍然无法解决。
Thanks in advance, Adam 预先感谢亚当
TableCell[] cella = new TableCell[100];
TableCell[] cellb = new TableCell[100];
This creates an Array but does not initialize its values. 这将创建一个数组,但不会初始化其值。 So
所以
cella[i].Text = nlst[i];
cellb[i].Text = alst[i];
fails because cella[i]
is always null
and .Text
does not exist (same applies for cellb[i]
). 失败,因为
cella[i]
始终为null
且.Text
不存在( cellb[i]
)。
You will have to initialize your array first or generate a new TableCell object in your loop 您将必须首先初始化数组或在循环中生成一个新的TableCell对象
cella[i] = new TableCell { Text = nlst[i] };
cellb[i] = new TableCell { Text = alst[i] };
Furthermore: 此外:
cellb[i] = new TableCell { Text = alst[i] };
looks like an error to me - N
goes to cell A
and A
goes to cell B
? N
转到单元格A
, A
转到单元格B
? using
statement when handling streams (and other IDisposable
objects). IDisposable
对象)时,请使用using
语句。 This makes sure the stream is properly disposed - even when errors occur. using(var reader = new StreamReader(Request.PhysicalApplicationPath + "/directory.txt");) { // your code her ... }
When you declare TableCell[] cella = new TableCell[100];
当您声明
TableCell[] cella = new TableCell[100];
you are creating an array of 100 references to TableCell
, all of which are null
. 您要创建一个由100个对
TableCell
引用组成的数组,所有引用均为null
。 If you try to execute cella[i].Text = nlst[i];
如果您尝试执行
cella[i].Text = nlst[i];
, cella[i]
is null
, so you get an exception when you try to assign null.Text
. ,
cella[i]
为null
,因此当您尝试分配null.Text
时会遇到异常。
It sounds like you need a loop to fill in values for all the elements of cella
and cellb
. 听起来您需要循环才能填充
cella
和cellb
所有元素的值。
You are never instantiating the TableCell
objects in that array; 您永远都不会实例化该数组中的
TableCell
对象。 you are only instantiating the array itself. 您只实例化数组本身。 You need to create
new TableCell()
objects for each entry before you can use their properties. 您需要为每个条目创建
new TableCell()
对象,然后才能使用它们的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.