8
Answers

Object reference not set to an instance of an object.

Photo of venus najad

venus najad

Aug 19
511
1

hello everyone, 

i try to fill a 2D paanel dynamically, but i receive this error:

Object reference not set to an instance of an object.

i have initilaized the controls and use following codes:

row=0, col=0;

 HtmlForm form2 = new HtmlForm();            
            Panel[,] panelAd = new Panel[mm, 3];
            Table mytable = new Table(); 

....

 mytable.Controls.Add(trow_7);

            if (col < 3)
            {
                panelAd[row, col].Controls.Add(mytable);
                this.form2.Controls.Add(panelAd[row, col]);
                col++;
            }
            else
            {
                row++;
                col = 0;
                panelAd[row, col].Controls.Add(mytable);
                this.form2.Controls.Add(panelAd[row, col]);
            }
            

how should i fix it? appreciate your resspond with codes, kind regards

 

Answers (8)

7
Photo of Gowtham Cp
691 1.3k 8.2k Aug 19

In your code, it seems you might be trying to use panelAd[row, col] before it's initialized.

Here's a revised code:

// Initialize variables
int row = 0, col = 0;
HtmlForm form2 = new HtmlForm();            
Panel[,] panelAd = new Panel[mm, 3]; // mm should be defined
Table mytable = new Table(); 

// Ensure all panels are initialized
for (int i = 0; i < mm; i++)
{
    for (int j = 0; j < 3; j++)
    {
        panelAd[i, j] = new Panel(); // Initialize each Panel
    }
}

// Your existing code for adding controls to panels
// Example loop to demonstrate filling panels
for (int i = 0; i < mm; i++)
{
    TableRow trow_7 = new TableRow(); // Assume this is initialized elsewhere

    mytable.Rows.Add(trow_7); // Assuming you are adding rows to the table

    if (col < 3)
    {
        panelAd[row, col].Controls.Add(mytable);
        form2.Controls.Add(panelAd[row, col]);
        col++;
    }
    else
    {
        row++;
        col = 0;
        panelAd[row, col].Controls.Add(mytable);
        form2.Controls.Add(panelAd[row, col]);
    }
}

// Add the form to the page or control container
this.Controls.Add(form2); // Or wherever you want to add form2
7
Photo of Amit Mohanty
16 52.2k 6.1m Aug 19

I think the error occurs because the Panel[,] panelAd array is created, but each individual Panel object inside that array is not initialized before you attempt to use it. Try the below code once:

int row = 0, col = 0;

HtmlForm form2 = new HtmlForm();
Panel[,] panelAd = new Panel[mm, 3];
Table mytable = new Table();

for (int i = 0; i < mm; i++)
{
    for (int j = 0; j < 3; j++)
    {
        panelAd[i, j] = new Panel(); // This initializes each panel
    }
}

mytable.Controls.Add(trow_7);

if (col < 3)
{
    panelAd[row, col].Controls.Add(mytable);
    this.form2.Controls.Add(panelAd[row, col]);
    col++;
}
else
{
    row++;
    col = 0;
    panelAd[row, col].Controls.Add(mytable);
    this.form2.Controls.Add(panelAd[row, col]);
}
5
Photo of Gowtham Cp
691 1.3k 8.2k Aug 20

Dear venus najad ,

Thanks for the follow-up. It seems the issue is due to using RepeatColumns and RepeatDirection properties with an asp:Panel, which doesn't support them.

To fix the layout, consider using a Table or GridView for better control over rows and columns. If you prefer to stick with panels, ensure they are properly initialized and added in the correct order:

for (int i = 0; i < mm; i++)
{
    for (int j = 0; j < 3; j++)
    {
        panelAd[i, j] = new Panel();
        panelAd[i, j].Controls.Add(mytable);
        form2.Controls.Add(panelAd[i, j]);
    }
}
this.Controls.Add(form2);

Also, make sure all controls needing postback are inside a form with runat="server".

Hope this helps!

3
Photo of venus najad
1.3k 414 32.7k Aug 21

dear Gowtham Cp

i forgot to tell, in my project i used     this.form2.Controls.Add(panels[row, col]); and then     this.Controls.Add(form2);

and i get that error...

when i use     form2.Controls.Add(panels[row, col]); (without this) and then         this.Controls.Add(form2);

i dont get any error, but the page is empty???!!!

i attach the file: 

3
Photo of venus najad
1.3k 414 32.7k Aug 21

dear Gowtham Cp

i have checked and everything is in order you have written... i do not understrand why it doesnt show the panel as matrix only as array with one column. i use gridview. when i use             this.Controls.Add(form2);          i get following error:

Control 'ctl01' of type 'LinkButton' must be placed inside a form tag with runat=server.

while my form is runat=server:        <HtmlForm ID="form2" runat="server">                how to fix the error????!

in the loop, i use              form2.Controls.Add(panelAd[row, col]);            is it because the panel shows as one column array???!!

i have deleted:

<asp:Panel ID="panels" runat="server" Height="150px" Width="150px" RepeatDirection="vertical" RepeatColumns="3" RepeatRows="2000" cellpadding="40px"></asp:Panel>

but the result is the same... have moved the loop inside a function, but thecsame result. i put           this.Controls.Add(form2); 

after the loop, but i got the same error again???!!!

observe that one of the controls s linkbutton, which has a click function and it fills inside the loop... the error indicates th linkbutton. aqter filling that control, i put     form2.Controls.Add(panelAd[row, col]);     an continue fill the rest of controls without using     form2.Controls.Add(panelAd[row, col]);  the result is the same (no matrix). 

i deleted     form2.Controls.Add(panelAd[row, col]);  from linkbutton control, but the same result....

unfortunately it is going to show only 10 rows... how to make new page (next pae) for the rest of rows in gridview?!

thanks if you respond with codes...

p.s. if you will i can attach  the file... 

3
Photo of Cr Bhargavi
176 11k 149.2k Aug 20
int row = 0, col = 0;
HtmlForm form2 = new HtmlForm();            
Panel[,] panelAd = new Panel[mm, 3];
Table mytable = new Table();

// Initialize all panels in the array
for (int i = 0; i < mm; i++)
{
    for (int j = 0; j < 3; j++)
    {
        panelAd[i, j] = new Panel(); // Initialize the panel
    }
}

// Add the table to the panels dynamically
mytable.Controls.Add(trow_7);

if (col < 3)
{
    panelAd[row, col].Controls.Add(mytable);
    form2.Controls.Add(panelAd[row, col]);
    col++;
}
else
{
    row++;
    col = 0;
    panelAd[row, col].Controls.Add(mytable);
    form2.Controls.Add(panelAd[row, col]);
}

The error occurs because the Panel objects in your array are not initialized. You need to initialize each Panel in the array with new Panel() before using them. Rest all your code remains same

3
Photo of venus najad
1.3k 414 32.7k Aug 19

dear mr Gowtham Cp and mr Amit Mohanty

thansk for your respond, it helped, but it deosnt show the page in matrix form. it shows yet in rows, no columns is created???!!!

<HtmlForm ID="form2" runat="server">                 
        <asp:Panel ID="panels" runat="server" Height="150px" Width="150px" RepeatDirection="Horizontal" RepeatColumns="3" RepeatRows="2000" cellpadding="40px"></asp:Panel>                
    </HtmlForm>

is it something wrong in panel definition? the size, direction or cellpadding? it seems none works???!!!

when i added:

this.Controls.Add(form2); i got error:

Control 'ctl01' of type 'LinkButton' must be placed inside a form tag with runat=server.

while it is already as you see above.... 

i have already 

this.form2.Controls.Add(panels[row, col]);

by which it shows ads in rows, nothingt in column??!!! i run 

panels[row, col].Controls.Add(mytable);
            this.form2.Controls.Add(panels[row, col]);
            col++;
            if (col == 3)
            {
                row++;
                col = 0;
            }

i8 appreciate your respond with codes, kind regards

2
Photo of venus najad
1.3k 414 32.7k Sep 03

hello everyone
 i have a gridview with 9000 rows. i fill my page dynamically in matrix form, withy 3000 rows and 3 columns, but i have 2 issues:
1. 
Panel[,]  panels=new Panel [(2000 / 3) + 1, 3];
Table mytable=new Table();
....
mytable.Controls.Add(trow_7);
panels[row, col] = new Panel();
panels[row, col].Controls.Add(mytable);
this.form2.Controls.Add(panels[row, col]);
            col++;
            if (col == 3)
            {
                row++;             
                    col = 0;
            }
the problem is that the page is not filled in matrixform, only rows with one column??!! 
when i use            form2.Controls.Add(panels[row, col]); and i use 
this.Controls.Add(form2);        after the loop for gridview, the page is empty???!!! may i ask you please tell me what is my misstake, thanks.

2.
i need that my page fills with 3 columns and 3 rows. i need the next page be created automatically, but it doesn't ??!! i use:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" AllowPaging="True" 
                  BackColor="White"  BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"  
                  CellSpacing="4" ForeColor="Black" GridLines="Vertical" Visible="false" AllowSorting="True" 
                  PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging"> 
        <PagerSettings Mode="NumericFirstLast" FirstPageText="First" NextPageText="Next" LastPageText="Last" />


 in code behind i use: 


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridView(0);

        }

but it shows only the three first members in gridview, and no next page is created??!! how should i solve this?

please help me, very kind regards