Solved

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

Posted on 2006-11-20
8
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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