Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4273462
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:42:59+00:00 2026-05-21T07:42:59+00:00

First here is my code: I have commented the problem lines protected void Page_Load(object

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:43:00+00:00Added an answer on May 21, 2026 at 7:43 am
    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 ... 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, to make my job explaining a bit easier, here's some of my code:
Well, this is my first post here and really enjoying the site. I have
Greetings to all! This is my first question here on stackoverflow. I have a
Here's my first question at SO. I have a internal application for my company
I have code which uses the StreamReader to read HTML from a file, then
first question here. I'm developing a program in C# (.NET 3.5) that displays files
This is my first post here and I wanted to get some input from
this is my first question here so I hope I can articulate it well
First question on here so please be nice :) I know very little about
I am new here so first of all my greetings to you I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.