Solved

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

Posted on 2006-11-20
8
225 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Finally almost got it 5 48
SqlDataBase 7 53
Jquery/JSON passing value from one function to another 12 60
linq, c# 8 22
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…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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