Solved

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

Posted on 2006-11-20
8
218 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 12

Expert Comment

by:AGBrown
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

18 Experts available now in Live!

Get 1:1 Help Now