Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#

Hallo,

I have defined one array of class in the main program and I get an error when I try to use it.

The definition in the main program is
C#
public const int NumColumns = 10;
public static GridViewComp[] gridView = new GridViewComp[NumColumns];

GridViewComp Class contains: string Title, int Width, bool Show.

In another form, I try to assign the values from an XML file
private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    int iLoop = 0;
    foreach (GridViewComp myComp in Program.gridView)
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        myComp.Title = item.Element("Title").Value;
        myComp.Width = Convert.ToInt32(item.Element("Width").Value);
        myComp.Show = Convert.ToBoolean(item.Element("Show").Value);
        iLoop++;
    }
}

(the xml part works fine)

When I execute the code, I become the following error:
"Object reference not set to an instance of an object" in the line
myComp.Title = item.Element("Title").Value;


I have looked for a solution, and I should initialize every element of the array, including the initialize instruction
private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    int iLoop = 0;
    foreach (GridViewComp myComp in Program.gridView)
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        myComp = new GridViewComp();
        myComp.Title = item.Element("Title").Value;
        myComp.Width = Convert.ToInt32(item.Element("Width").Value);
        myComp.Show = Convert.ToBoolean(item.Element("Show").Value);
        iLoop++;
    }
}


But when I do so, the compiler shows the following error:
myNamespace.GridViewComp.GridViewComp() is inaccessible due to its protection level
and marks the underlined part.

I also tried with a "for" loop instead of "foreach"
private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    for (int iLoop = 0; iLoop < Program.NumColumns; iLoop++ )
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        Program.gridView[iLoop] = new GridViewComp();
        Program.gridView[iLoop].Title = item.Element("Title").Value;
        Program.gridView[iLoop].Width = Convert.ToInt32(item.Element("Width").Value);
        Program.gridView[iLoop].Show = Convert.ToBoolean(item.Element("Show").Value);
    }
}

but the result is the same.

Has anybody any idea of how can I fill the array with the xml data, or what I could be doing wrong?

Thank you very much in advance.

David
(PS: I use MS VS2010)
Posted
Updated 18-Mar-13 3:02am
v4

Hi,

Exception is because Object value is not assigned and you are using from that index.

Make sure you have gridView object with filled information before accessing its index.

hope this help you to identify the issue.

thanks
-Amit
 
Share this answer
 
Comments
NeueStudium2012 18-Mar-13 9:07am    
Hallo Amit,
That is why I tryed with the initializing sentence.
But I do not know the values before reading them from the xml file.
AmitGajjar 18-Mar-13 9:11am    
In that case you can assign default value, if it is of Integer Array then you can assign zero for length that you want. if you will not assign any value and try to access it's value at particular index then it will gives you object reference exception because it's memory allocation is not done. memory allocation will done only after assigning value.
NeueStudium2012 18-Mar-13 9:45am    
and how can I do that?
It is not so simple, it is a class, and the number of elements can be large, so it need a loop to do so.
But with the initializing sentence, the compiler drops an error.
AmitGajjar 19-Mar-13 1:47am    
if you do not have constraint to use Array then you can use List as well. use one constructor to fill your list/Array.
Finally, I got one solution:

C#
private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    for (int iLoop = 0; iLoop < Program.NumColumns; iLoop++ )
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        Program.gridView[iLoop] = new GridViewComp(
            item.Element("Title").Value, 
            Convert.ToInt32(item.Element("Width").Value), 
            Convert.ToBoolean(item.Element("Show").Value));
    }
}


Thank you everybody for your comments.

David
 
Share this answer
 
Finally, I got one solution:

The values must be initialized within the round brackets, instead of initialize the array and later on assign the values.

C#
private void AssignGridSetup()
{
    var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
    for (int iLoop = 0; iLoop < Program.NumColumns; iLoop++ )
    {
        var item = columns.Element(Program.columnElement[iLoop]);
        Program.gridView[iLoop] = new GridViewComp(
            item.Element("Title").Value, 
            Convert.ToInt32(item.Element("Width").Value), 
            Convert.ToBoolean(item.Element("Show").Value));
    }
}


Thank you everybody for your comments.

David
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900