Adam Schiavone
Adam Schiavone

Reputation: 2452

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 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

Upvotes: 1

Views: 757

Answers (3)

Zebi
Zebi

Reputation: 8882

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]).

You will have to initialize your array first or generate a new TableCell object in your loop

cella[i] = new TableCell { Text = nlst[i] };
cellb[i] = new TableCell { Text = alst[i] };

Furthermore:

  • Consider using LINQ to handle list operations and
  • Try renaming your variables to more meaningful ones. cellb[i] = new TableCell { Text = alst[i] }; looks like an error to me - N goes to cell A and A goes to cell B?
  • Use the using statement when handling streams (and other IDisposable objects). This makes sure the stream is properly disposed - even when errors occur.
    using(var reader = new StreamReader(Request.PhysicalApplicationPath +  "/directory.txt");) 
    {
        // your code her ... 
    }

Upvotes: 4

Andrew Barber
Andrew Barber

Reputation: 40139

You are never instantiating the TableCell objects in that array; you are only instantiating the array itself. You need to create new TableCell() objects for each entry before you can use their properties.

Upvotes: 0

Gabe
Gabe

Reputation: 86698

When you declare TableCell[] cella = new TableCell[100]; you are creating an array of 100 references to TableCell, all of which are null. If you try to execute cella[i].Text = nlst[i];, cella[i] is null, so you get an exception when you try to assign null.Text.

It sounds like you need a loop to fill in values for all the elements of cella and cellb.

Upvotes: 1

Related Questions