简体   繁体   中英

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

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM