Solved

Accessing an Array of Tables in C#,throws Object reference not set to an instance of an object exception

Posted on 2007-12-04
4
394 Views
Last Modified: 2013-11-26
I'm working on a web based report(ASP.NET 1.1 & C#)  and  essentially have an array of tables
which i am filling dynamically with rows and Cells from the results of a DataTable

Below is the a small sample that illustrates this.

For Starters.I'm not really sure if i can actually create an array of Tables
but i needed something that offered that functionality,and couldn't think of
any other option fast enough

The problem i'm having is i get an "Object reference Not set to an Instance of an object"
exception whenever i get to
1) td10[i].Text  or 2) Tbl[i].Rows.Add(trow)

Am perplexed.Please Advice

Table[] Tbl= new Table[count];
TableRow[] trow = new TableRow[count];						TableCell[] td10=new TableCell[count];
for(int i =0; i< count; i++)
{
										td10[i].Text="Hey";
							td10[i].Width=150;
						  trow[i].Cells.Add(td10[i]);                         
 
Tbl[i].Rows.Add(trow[i]);
 
}
						}

Open in new window

0
Comment
Question by:PremGss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 20409173
when you declare an array of objects, like
      TableCell[] td10 = new TableCell[count]
the array object is created, but the elements into it are null. So you cannot access them before constructing a new element. Something like:

Table[] Tbl= new Table[count];
TableRow[] trow = new TableRow[count];
TableCell[] td10 = new TableCell[count];
for(int i =0; i< count; i++)
{
      td10[i] = new TableCell();   // this is required to avoid the exception
      td10[i].Text="Hey";
      td10[i].Width=150;

      trow[o] = new TableRow();   // required
      trow[i].Cells.Add(td10[i]);

      Tbl[i] = new Table();  // required
      Tbl[i].Rows.Add(trow[i]);
 }
      
besides this, your code looks strange, if count = 20, you create 20 tables, 20 rows and 20 cells.
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 20409210
you may have a dataset to hold all tables and then process tables one by one from dataset.. like this
//
DataSet ds=new DataSet("all tables");
ds.Tables.add("Table1);
ds.Tables.add("Table2);
ds.Tables.add("Table3);

int count=0;
int Rowcount=9;
for(;ds.tables.count > count; count++)
{
//here if you know how many rows to add in each table then you may use inner loop to do that
otherwise.
ds.tables[0].rows.add(xxxxxxxxxxxxxxxx);

}


thanks
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20592635
Forced accept.

Computer101
EE Admin
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question