469,623 Members | 899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Array Design

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.
Nov 17 '05 #1
7 1305
Adam Suszeck <AS*********@msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Adam Suszeck <AS*********@msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Adam Suszeck <AS*********@msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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" <AS*********@msn.com> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Adam Suszeck <AS*********@msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 17 '05 #5
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[i] = 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.
Nov 17 '05 #6
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[i] = 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.
Nov 17 '05 #7
Thanks guys,

I've sorted this problem now.

-Adam

"laimis" <si*****@iit.edu> wrote in message news:42**************@iit.edu...
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[i] = 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.

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Christian Hackl | last post: by
4 posts views Thread by christiang | last post: by
12 posts views Thread by Maxwell2006 | last post: by
18 posts views Thread by toton | last post: by
272 posts views Thread by Peter Olcott | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
12 posts views Thread by raylopez99 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.