Solved

500pts: Creating an array of my class and assigning value gives error

Posted on 2006-11-20
8
219 Views
Last Modified: 2010-04-16
Hi there,

Can anybody tell me why assigning a value to an array of clases i have created would give the following error?

Unhandled exception: Object reference not set to an instance of an object.

Here is my line to initialise the array with 300

Perfil.Preference[] localPreferences = new Perfil.Preference[300];

and then assignin this gives the error, the totalFiles at this point is 0 and the id = 11

localPreferences[totalFiles].ID = id;

Any ideas?

must be doing something wrong,

Thanks in advance for any info or help

Regards

Ian
0
Comment
Question by:ianinspain
  • 4
  • 2
  • 2
8 Comments
 
LVL 12

Accepted Solution

by:
AGBrown earned 250 total points
ID: 17980752
you need to actually put an instance of my class in at the position, before you can set its ID:

localPreferences[totalFiles] = new Preference();
localPreferences[totalFiles].ID = id;

Andy
0
 

Author Comment

by:ianinspain
ID: 17980793
wow thanks andy... can't believe i missed that... is there anyway to do this all together ... or do i have to do each one separately?

Thanks

Ian
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 17981102
Ian,

No problem. The only way is to iterate over the array once you created the array:
for (int i = 0; i < localPreferences.Length; i++)
{
    localPreferences[i] = new Preference();
}

However, if you aren't sure that you are going to use every element, then I would just create the Preferences class as you need it.

I might be telling you things you already know here, but if not then they're worth knowing. If your array is going to change length a lot and you aren't trying to squeeze every last nanosecond of performance out of the application, then I would move the array to something like a List<Preference> (.NET 2.0), or an ArrayList (.NET 1.1). Similarly, if you are going to be doing other things to the array, like referring to each Preference through it's ID, and therefore searching the array a lot, I would ensure that the array is sorted by ID so that you can take advantage of BinarySearch to find each item (its a lot quicker). .NET 2.0 allows this through use of SortedList<Preference>. You can write a custom comparer for the Preference class to do the searches and sort comparisons on ID, or any other field you like.

Andy
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 250 total points
ID: 17982412
You could also declare Preference[] as a STATIC member of the class.  Then you do not need to create an instance of the class.

But this depends on how the array is actually being used in your application.  Does each instance of Perfil need a separate array, or could all instances use the same array?

AW
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:AGBrown
ID: 17982707
Hi Arthur,

I'm interested in what you've said, but I'm not sure I understand. Surely if you create an array (localPreferences) of classes (Preference), you still need to put an instance of that class (Preference) into each element of the array (localPreferences) before you can use it, regardless of whether its the array is a static field or an instance field.

Or are you speaking with respect to the Perfil class, in which case although you don't need to create an instance of Perfil if localPreferences is a static field, you still need to assign the array to localPreferences field and an instance of the Preferences class into each element of the array.

Then as a seperate issue, if you only need a single instance of that localPreferences array for all threads and objects, it might be a case of deciding between a static class, or a singleton instance of a class. Arguably if Perfil is necessarily an instance class, not static, then maybe the static field for the array should be in a seperate class which is static/singleton to help keep coherence in the design? That would also help ensure that if you needed to put synchronisation etc. in place you would be able to segregate locking mechanisms or access methods to the central array resource away from the purpose of the Perfil class?

Andy
0
 

Author Comment

by:ianinspain
ID: 17997204
AGBrown,

thank you... i am ready to assign points here or shall i wait until Arthur replies?

Ian
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 17997443
Up to you, I don't mind. I think Arthur probably had more than a valid point - he is pretty good at this stuff! I just wanted to understand it.

Andy
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17997705
that was my point about how the array is actually used.  If the array serves a One copy for all role, then make it a Static member, otherwise it would be ab instance member.

AW
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IO.FileSystemWatcher dies when used in Powershell 1 44
Access properties in nested observable collections 8 23
Runtime Error 2 27
Angular - break the dataset in chunks? 7 38
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

23 Experts available now in Live!

Get 1:1 Help Now