|
-
Nov 22nd, 2004, 04:44 PM
#1
Making an object:
Now I am going to start as it looks like Mendhak have done lately....here is the next question.
I am trying to make a new object of a class. The class uses delegations. The class looks like this:
Code:
using System;
namespace Spaceobj {
public delegate void trackOfDay(int p_iDay);
public class cClock {
public cClock(int p_iDay)
{
iDay = p_iDay;
}
public int iDay;
// Event to tell the planets to move.
public event trackOfDay moveAcordingly;
// Angi ny dag
public void NyDag(int p_iDay) {
iDay = (iDay + 1) % 365;
moveAcordingly(iDay);
}
};
}
But when I simply try to make an object of it, like this:
Code:
cClock earthDay = new cClock(1);
then it compiles fine, but when I run it, then it crashes saying.
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an object.
Last edited by NoteMe; Nov 22nd, 2004 at 05:06 PM.
-
Nov 22nd, 2004, 05:05 PM
#2
This starts to irritate me...first off because I can't compile one and one file, second, it was not that line that caused the error at all.....
-
Nov 22nd, 2004, 05:06 PM
#3
Frenzied Member
I could reproduce your error with that code from your other recent thread. Although that line is highlighted, that's not where the error is coming from, it's coming from this:
Code:
foreach (Himmellegeme spaceObject in skyObjects)
You see, skyObject is still null when you get to this point. If you were to do all that stuff in the constuctor instead, you can get rid of this problem also.
-
Nov 22nd, 2004, 05:06 PM
#4
Originally posted by Mike Hildner
I could reproduce your error with that code from your other recent thread. Although that line is highlighted, that's not where the error is coming from, it's coming from this:
Code:
foreach (Himmellegeme spaceObject in skyObjects)
You see, skyObject is still null when you get to this point. If you were to do all that stuff in the constuctor instead, you can get rid of this problem also.
Posted at the same time, didn't we...
-
Nov 22nd, 2004, 05:09 PM
#5
Frenzied Member
I think you beat me by a minute - I have a hard time reading Norwegian
-
Nov 22nd, 2004, 05:12 PM
#6
Frenzied Member
Anyway, I hope that makes sense. In your main method, you say "run my Form1 application", it runs, but it isn't until later on (in the main method) that skyObjects actually gets instantiated, hence the null reference business.
Sometimes you get parked on the next line of code that caused the error, and not on the line that caused it. Sometimes you don't, and I haven't figured out why the difference.
-
Nov 22nd, 2004, 05:22 PM
#7
-
Nov 22nd, 2004, 05:25 PM
#8
Frenzied Member
You're welcome, and I'll ask for help if I need it - thanks. Keep in mind that the problems you were running into are problems everybody runs into at first - newbie type problems that are frustating, but easy to get over. Not too bad for a couple of hours 
Good luck,
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|