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
382 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 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now