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
This creates an Array but does not initialize its values. So
fails because
cella[i]is alwaysnulland.Textdoes 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 –Ngoes to cellAandAgoes to cellB?usingstatement when handling streams (and otherIDisposableobjects). This makes sure the stream is properly disposed – even when errors occur.using(var reader = new StreamReader(Request.PhysicalApplicationPath + "/directory.txt");) { // your code her ... }