Hi
Ive got an error message saying "Object reference not set to an instance of an object." It doesnt make sense to me and the troublshooting tips says to make a new keyword, but since Im new to C# I dont fully understand it. Heres the bit of code thats making the problem. I think I know why the errors coming up because Ive linked two classes together so I might not have done it right. Can someone help me with this please? I just can't seem to figure it out.

class Athletes
    { 
private Event myEventParticipating;
public Event EventPart
        {
            get
            {
                return myEventParticipating;
            }
            set
            {
                myEventParticipating = value;
            }
        }
}
class Event
    {
private string myName = "Shotput";
 public string Name
        {
            get
            {
                return myName;
            }
            set
            {
                myName = value;
            }
        }
}
Console.WriteLine("=====Athlete 1 - Details=====");
            Console.WriteLine("");
            Console.WriteLine("Enter First Name of Athlete 1:");
            athlete1.FirstName = Console.ReadLine();
            Console.WriteLine("Enter Last Name of Athlete 1:");
            athlete1.LastName = Console.ReadLine();
            Console.WriteLine("Enter Address of Athelete 1:");
            athlete1.Address = Console.ReadLine();
            Console.WriteLine("Enter Phone Number of Athlete 1:");
            athlete1.PhoneNumber = Console.ReadLine();//Up to here these lines all work fine
            Console.WriteLine("Enter the Event that Athlete 1 is participating in:");
  [U][B]athlete1.EventPart.Name = Console.ReadLine();[/B][/U]         
   
Console.WriteLine("Thankyou, Athlete 1 has now been saved.");

The underlined code is the line that comes up with the error that I cant figure it out where Ive gone wrong. Any help will be appreciated, and thanks in advance.

Recommended Answers

All 6 Replies

myEventParticipating is null by default and you never set it in the snippets.

You never have something like Athletes athlete1 = new Atletes();
This tells the compiler to reserve some memory space to hold the info about athlete1.
Because Event is a reserved keyword in C#, I don't think your code will work as you intended. I should change the word into Meeting or Happening or something.

@ Deceptikon - What do you mean by null? I declare the EventPart as Console.ReadLine?
@Ddanbe - The reason I have the athlete1 is because I have more than one athlete and also the Event keyword works fine because my other Code works with Event. Can you see anything else where I might be going wrong.
But thankyou for your quick replies, its much appreciated

Because Event is a reserved keyword in C#, I don't think your code will work as you intended.

C# is case sensitive. event is a reserved keyword, but Event is not. It may be a standard class, but I don't think so.

@ Deceptikon - What do you mean by null?

I mean this:

private Event myEventParticipating;

Is equivalent to this:

private Event myEventParticipating = null;

If you never say athelete1.EventPart = new Event() or something similar, then athelete.EventPart is equal to null and you cannot access the members of a null object.

C# is case sensitive

Yes, I know, I find it one of its strengths.
This is a perfect example how the use of reserved words, otherwise spelled, can confuse people(in this case me!)
Thanks for reminding me anyway. :)

Hi the problem has been solved for now, I think I solved the problem by creating a new object such as

Event event1 = new Event();
            Event event2 = new Event();

and then saving Event 1 and event 2 to the same array and that for some reason solved the problem. Thanks everyone for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.