Results 1 to 8 of 8

Thread: Making an object:

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Resolved 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.

  2. #2

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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.....

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I think you beat me by a minute - I have a hard time reading Norwegian

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Mike Hildner
    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.

    Yeah... the error is clear, but the error handling by the IDE coudn't have been worse. But as long as I am aware of it I think I can handle it.

    Thanks


    Originally posted by Mike Hildner
    I think you beat me by a minute - I have a hard time reading Norwegian
    I started out with everything in Norwegian. But started to convert my project to English to post the code examples, but I didn't have the time to rewrite it all when you asked if I wanted to post the whole project.

    At least thanks for ALL your help today. You have been great. Just ask me if you need any DX help..thats what I do..


    This is it for tonight. I will leave you here with how far I got the first 2-3 hours with C#......hope I get more time tomorrow.



  8. #8
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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
  •  



Click Here to Expand Forum to Full Width