Array Design

  • Thread starter Thread starter Adam Suszeck
  • Start date Start date
A

Adam Suszeck

I have an application that has a state class, which basically stores
application information for other forms to access during the execution of
the application.

In state, I store an array which contains a number of positions. Depending
on what the user is doing in the application, this may hold 10 or 20 items.

The array length and items are set by another form in the application, ie.
not the state class.

The trouble is, I'm running into problems with null reference pointers. So
despite checking first off that the array is set and is not empty, i still
have exceptions cropping up.

Should I just wrap this in a try catch block and handle any exceptions, or
is there a better way of doing this?

Many thanks.
 
Adam Suszeck said:
I have an application that has a state class, which basically stores
application information for other forms to access during the execution of
the application.

In state, I store an array which contains a number of positions. Depending
on what the user is doing in the application, this may hold 10 or 20 items.

The array length and items are set by another form in the application, ie.
not the state class.

The trouble is, I'm running into problems with null reference pointers. So
despite checking first off that the array is set and is not empty, i still
have exceptions cropping up.

Should I just wrap this in a try catch block and handle any exceptions, or
is there a better way of doing this?

Using a try/catch block is almost certainly the wrong way of going. You
need to understand exactly why you're getting the exceptions first -
and unfortunately, it's hard to help on that front without seeing some
code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Thanks Jon.

I'll try and explain things a bit better.

The application is fairly complex and uses quite a lot of forms, so I'll
simplify things to focus on the issue i'm having.

When the application starts I initialise a state class, which contains
details about the user and other information. It also contains an array of
members

State Class:
public Member[] members;

Form1 Class:
I ask the user how many members to create. Ie. 10 or 20.
in the case of 10 i initialise the array

this.state.members = new Member[10];

Form2 Class:
This class basically allows the user to view the members that are currently
in the application.
If the array is null, then there are no members and hence no members can be
displayed.

if the members array has been initialised and populated then the members
will be displayed.

The reason i store members in state, is because I need to use the array in
other forms, and it's an easier way of making the data available to other
forms that may require it.

I hope this explains what I'm trying to do.

Regards,
Adam
 
Adam Suszeck said:
I'll try and explain things a bit better.

The application is fairly complex and uses quite a lot of forms, so I'll
simplify things to focus on the issue i'm having.

When the application starts I initialise a state class, which contains
details about the user and other information. It also contains an array of
members

State Class:
public Member[] members;

Form1 Class:
I ask the user how many members to create. Ie. 10 or 20.
in the case of 10 i initialise the array

this.state.members = new Member[10];

Form2 Class:
This class basically allows the user to view the members that are currently
in the application.
If the array is null, then there are no members and hence no members can be
displayed.

if the members array has been initialised and populated then the members
will be displayed.

The reason i store members in state, is because I need to use the array in
other forms, and it's an easier way of making the data available to other
forms that may require it.

I hope this explains what I'm trying to do.

Yes, but it doesn't explain why you're getting an exception. You'll
have to provide some code for that.

Note that just creating the array doesn't populate it with members -
each element will be null to start with.
 
Hi,


Do as Jon said, post some code of where you are getting the exception. as
well as the exception Message and/or StackTrace to give a better idea of
where you have the problem

ITOH it's pretty sure you are referencing an element in the array that you
have not initializated.


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Adam Suszeck said:
Thanks Jon.

I'll try and explain things a bit better.

The application is fairly complex and uses quite a lot of forms, so I'll
simplify things to focus on the issue i'm having.

When the application starts I initialise a state class, which contains
details about the user and other information. It also contains an array
of members

State Class:
public Member[] members;

Form1 Class:
I ask the user how many members to create. Ie. 10 or 20.
in the case of 10 i initialise the array

this.state.members = new Member[10];

Form2 Class:
This class basically allows the user to view the members that are
currently in the application.
If the array is null, then there are no members and hence no members can
be displayed.

if the members array has been initialised and populated then the members
will be displayed.

The reason i store members in state, is because I need to use the array in
other forms, and it's an easier way of making the data available to other
forms that may require it.

I hope this explains what I'm trying to do.

Regards,
Adam
 
Yeah, the guys are right, when you do this:

this.state.members = new Member[10];

the this.state.members all ten elements are null. You have initialized
the members array, but not the elements inside it (something like
members = new Member(); )

To avoid null exceptions when you access each member, just do this:

if (members[currentIndex] != null)
{
// this member is not null, let's use it
}

Anywyas, you migth still want to post your code so that we can see for
sure that that is the problem.
 
Yeah, the guys are right, when you do this:

this.state.members = new Member[10];

the this.state.members all ten elements are null. You have initialized
the members array, but not the elements inside it (something like
members = new Member(); )

To avoid null exceptions when you access each member, just do this:

if (members[currentIndex] != null)
{
// this member is not null, let's use it
}

Anywyas, you migth still want to post your code so that we can see for
sure that that is the problem.
 
Back
Top