Newbie: BitArray initilization

G

Guest

I am getting runtime errors accessing BitArray objects with the error being;

Object reference not set to an instance of an object

I though the 'new' operator created the instance and everything was all set to go?
What is the correct way to set up the bit arrays?

Code that shows this problem is listed below;

using System;
using System.Collections;

// See 7.5.10.2 Array creation expressions in the C# language specification

public struct My_sru
{
public BitArray[] My_1d_ary;
public BitArray[][] My_2d_ary;
public BitArray[,] My_2d_rectangular_ary;
}

public class Test_compile_cls
{

static My_sru s_my_sru;

[STAThread]
public static void Main(string[] p_command_line_arguments_str_ary)
{
//#---1---- Declare and set local state space --------------------------------------------:_:---#

int l_idx = 0;

s_my_sru.My_1d_ary = new BitArray [10];
s_my_sru.My_2d_ary = new BitArray [] [] {new BitArray[10], new BitArray[10]};
s_my_sru.My_2d_rectangular_ary = new BitArray [10,10];

//#---2---- Initilize arrays -------------------------------------------------------------:_:---#

s_my_sru.My_1d_ary[0].Length = 32;

// Object reference not set to an instance of an object

s_my_sru.My_1d_ary [1].SetAll( false);
s_my_sru.My_2d_ary [2] [2].SetAll( true);
s_my_sru.My_2d_rectangular_ary[3,3].SetAll( true);

for( l_idx = 0; l_idx < 10; l_idx ++)
{
s_my_sru.My_1d_ary [l_idx].SetAll( false);
}

//#---3---- Set several bits -------------------------------------------------------------:_:---#

s_my_sru.My_2d_rectangular_ary[3,3].Set(0,false);
s_my_sru.My_2d_rectangular_ary[3,3].Set(1,true);
s_my_sru.My_2d_rectangular_ary[3,3].Set(2,false);
s_my_sru.My_2d_rectangular_ary[3,3].Set(3,true);

//#---4---- Return -----------------------------------------------------------------------:_:---#

} // end main

} // end class
 
G

Guest

You have initialized the array of BitArray objects, but not the BitArray objects.
Replace this part of your code
s_my_sru.My_1d_ary[0].Length = 32;

// Object reference not set to an instance of an object

s_my_sru.My_1d_ary [1].SetAll( false);
s_my_sru.My_2d_ary [2] [2].SetAll( true);

with this one:
s_my_sru.My_1d_ary[0] = new BitArray(32);
//s_my_sru.My_1d_ary[0].Length = 32;
// Object reference not set to an instance of an object
s_my_sru.My_1d_ary [1] = new BitArray(32, false);
//s_my_sru.My_1d_ary [1].SetAll( false);

and the 'object reference not set' error occurs on statement after the above code. So you can use various BitArray constructors and initialize the array elements before using them.
Hope this helps.
 
G

Guest

Just thought I would post the final solution FYI to those working with BitArray.

Final solution to 2 d array init with and witout a loop;

using System;
using System.Collections;

public struct My_sru
{
public BitArray[][] My_2d_ary; // [one] [two]
}

public class Test_compile_cls
{
static My_sru s_1_sru;
static My_sru s_2_sru;

[STAThread]
public static void Main(string[] p_args)
{
int l_size_limit_one_ary_siz = 10;
int l_size_limit_two_ary_siz = 10;
int l_one_idx = 0;
int l_two_idx = 0;

// Without loop : A 10 x 10 2d array ( init still incomplete )

s_1_sru.My_2d_ary = new BitArray [] [] {
new BitArray[10], // 0
new BitArray[10], // 1
new BitArray[10], // 2
new BitArray[10], // 3
new BitArray[10], // 4
new BitArray[10], // 5
new BitArray[10], // 6
new BitArray[10], // 7
new BitArray[10], // 8
new BitArray[10] // 9
};

// Using loop : A 10 x 10 2d array ( init complete )

s_2_sru.My_2d_ary = new BitArray [l_size_limit_one_ary_siz] [];

for( l_one_idx = 0;
l_one_idx < l_size_limit_one_ary_siz;
l_one_idx ++
){
s_2_sru.My_2d_ary[ l_one_idx ] = new BitArray [ l_size_limit_two_ary_siz ];

for( l_two_idx = 0;
l_two_idx < l_size_limit_two_ary_siz;
l_two_idx ++
){
s_2_sru.My_2d_ary[ l_one_idx ] [ l_two_idx ] = new BitArray (32, false);
}
}

} // end main
} // end class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top