对明显非空对象的空引用

发布于 2024-10-31 13:43:26 字数 1384 浏览 1 评论 0原文

首先是我的代码:

我已经评论了我检查过的问题行

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾`听者〃 2024-11-07 13:43:26
TableCell[] cella = new TableCell[100];
TableCell[] cellb = new TableCell[100];

这会创建一个数组,但不会初始化其值。因此

cella[i].Text = nlst[i];
cellb[i].Text = alst[i];

失败,因为 cella[i] 始终为 null 并且 .Text 不存在(同样适用于 cellb[i])。

您必须首先初始化数组或在循环中生成新的 TableCell 对象

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

此外:

  • 考虑使用 LINQ 来处理列表操作并
  • 尝试将变量重命名为更有意义的变量。 cellb[i] = new TableCell { Text = alst[i] }; 对我来说看起来像是一个错误 - N 转到单元格 AA 转到单元格 B
  • 处理流(和其他 IDisposable 对象)时使用 using 语句。这可以确保正确处理流 - 即使发生错误。
    using(var reader = new StreamReader(Request.PhysicalApplicationPath +  "/directory.txt");) 
    {
        // your code her ... 
    }
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 ... 
    }
恏ㄋ傷疤忘ㄋ疼 2024-11-07 13:43:26

当您声明 TableCell[] cella = new TableCell[100]; 时,您将创建一个包含 100 个对 TableCell 引用的数组,所有这些引用均为 null。如果您尝试执行 cella[i].Text = nlst[i];cella[i]null,因此您会得到当您尝试分配 null.Text 时出现异常。

听起来您需要一个循环来填充 cellacellb 的所有元素的值。

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.

清晰传感 2024-11-07 13:43:26

您永远不会实例化该数组中的 TableCell 对象;您只是实例化数组本身。您需要为每个条目创建 new TableCell() 对象,然后才能使用它们的属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文