Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
397 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
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 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:Munawar Hussain
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

971 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