• 0

An error occurring during compiling


Question

i'm building an Hide and seek game using csharp here is the code, it has 2 interfaces , 2 independent classes, 6 other classes which inherit from the interface and the other 2 classes, the code has zero errors and runs when pressed start. But some times during execution the code starts running and give me this error it pints to the line currentLocation.Exits.Name

 

So hears the cod for the entire program

  interface IHasExteriorDoor
    {
        string DoorDescription { get; }
        Location DoorLocation { get; set; }
    }
 interface IHidingPlace
    {
        string HidingPlaceName { get; }
    }
abstract class Location
    {
        public Location(string name)
        {
            this.name = name;
        }
        public Location[] Exits;
        private string name;
        public string Name
        {
            get { return name; }
        }
        public virtual string Description
        {
            get
            {
                string description = " You're standing in the " + name + ". You see exits to the following places:";

                for (int i = 0; i < Exits.Length; i++)
                {
                    description += "" + Exits[i].Name;
                    if (i != Exits.Length - 1)
                        description += ",";
                }
                description += ".";
                return description;
            }
        }
    }
class Opponent
    {
        private Random random;
        private Location myLocation;
        public Opponent(Location startingLocation)
        {
            myLocation = startingLocation;
            random = new Random();
        }
        public void Move()
        {
            if (myLocation is IHasExteriorDoor)
            {
                IHasExteriorDoor LocationWithDoor = myLocation as IHasExteriorDoor;
            
            if (random.Next(2) == 1)
                myLocation = LocationWithDoor.DoorLocation;
        }
        bool hidden = false;
        while(!hidden)
            {
            int rand = random.Next(myLocation.Exits.Length);
            myLocation = myLocation.Exits[rand];
            if(myLocation is IHidingPlace)
            hidden = true;
            }
        }
        public bool Check(Location locationToCheck)
        {
            if (locationToCheck != myLocation)
                return false;
            else
                return true;
        } 
    }
 class OutsideWithDoor : Outside, IHasExteriorDoor
    {
        public OutsideWithDoor(string name, bool hot, string doorDescription): base(name, hot)
            {
            this.doorDescription = doorDescription;
            }
        private string doorDescription;
        public string DoorDescription
        {
            get { return doorDescription; }
        }
        private Location doorLocation;
        public Location DoorLocation
        {
            get { return doorLocation; }
            set { doorLocation = value; }
        }
        public override string Description
        {
            get
            {
                return base.Description + "You see " + doorDescription + ".";
            }
        }
    }
 class Outside : Location
    {
        private bool hot;
        public bool Hot { get { return hot; } }

        public Outside(string name, bool hot) : base(name)
        {
            this.hot = hot;
        }
        public override string Description
        {
            get
            {
                string NewDescription = base.Description;
                if (hot)
                    NewDescription += "It's very hot.";
                return NewDescription;
            }
        }
    }
 class OutsideWithHidingPlace : Outside, IHidingPlace
    {
        public OutsideWithHidingPlace(string name, bool hot, string hidingPlaceName) : base(name, hot)
        { this.hidingPlaceName = hidingPlaceName; }

        private string hidingPlaceName;
        public string HidingPlaceName
        {
            get { return hidingPlaceName; }
        }
        public override string Description
        {
            get { return base.Description + "Someone could hide" + hidingPlaceName + "."; }
        }
    }
class RoomwithDoor: RoomWithHidingPlace, IHasExteriorDoor
    {
        public RoomwithDoor(string name, string decoration, string hidingPlaceName, string doorDescription):base(name, decoration, hidingPlaceName)
        { this.doorDescription = doorDescription; }
        private string doorDescription;
        public string DoorDescription
        {
            get { return doorDescription; }
        }
        private Location doorLocation;
        public Location DoorLocation
        {
            get { return doorLocation; }
            set { doorLocation = value; }
        }
    }
class Room: Location
    {
        private string decoration;

        public Room(string name, string decoration):base(name)
        {
            this.decoration = decoration;
        }
        public override string Description
        {
            get
            {
                return base.Description+"You see " + decoration + ".";
            }
        }
    }
 class RoomWithHidingPlace : Room, IHidingPlace
    {
        public RoomWithHidingPlace (string name, string decoration, string hidingPlaceName): base(name, decoration)
        {
            this.hidingPlaceName = hidingPlaceName;
        }
        private string hidingPlaceName;
        public string HidingPlaceName
        {
            get { return hidingPlaceName; }
        }
        public override string Description
        {
         get { return base.Description + "Someone could hide" + hidingPlaceName + "."; }
        }
    }
public partial class Form1 : Form
    {
        int Moves;
        Location currentLocation;

        RoomwithDoor livingRoom;
        RoomWithHidingPlace diningRoom;
        RoomwithDoor kitchen;
        Room stairs;
        RoomWithHidingPlace hallway;
        RoomWithHidingPlace bathroom;
        RoomWithHidingPlace masterBedroom;
        RoomWithHidingPlace secondBedroom;

        OutsideWithDoor frontYard;
        OutsideWithDoor backYard;
        OutsideWithHidingPlace garden;
        OutsideWithHidingPlace driveway;

        Opponent opponent;

        public Form1()
        {
            InitializeComponent();
            CreateObjects();
            opponent = new Opponent(frontYard);
            ResetGame(false);
            MoveToANewLocation(livingRoom);
        }
        private void MoveToANewLocation(Location newLocation)
        {
            Moves++;
            currentLocation = newLocation;
            RedrawForm();
        }
        private void RedrawForm()
        {
            exits.Items.Clear();
            for (int i = 0; i < currentLocation.Exits.Length; i++)
                exits.Items.Add(currentLocation.Exits[i].Name);
            exits.SelectedIndex = 0;
            description.Text = currentLocation.Description + "\r\n(move #" + Moves + ")";
            if(currentLocation is IHidingPlace)
            {
                IHidingPlace hidingPlace = currentLocation as IHidingPlace;
                exits.Text = "Check" + hidingPlace.HidingPlaceName;
                exits.Visible = true;
            }
            else
                exits.Visible = false;
                if (currentLocation is IHasExteriorDoor)
                    goThroughTheDoor.Visible = true;
                else
                    goThroughTheDoor.Visible = false;
        }
        private void CreateObjects()
        {
            livingRoom = new RoomwithDoor("Living Room", "an antique carpet","inside the closet", "an oak door with a brassknob");
            diningRoom = new RoomWithHidingPlace("Dining Room", "a crystal chandelier", "in the tall armorie");
            kitchen = new RoomwithDoor("Kitchen", "stainless steel appliances","in the cabinet", "a screen door");
            stairs = new RoomwithDoor("Kitchen", "stainless steel appliances", "in teh cabinet", "a screen door");
            hallway = new RoomWithHidingPlace("Upstairs Hallway", "a picture of a dog", "in the closet");
            bathroom = new RoomWithHidingPlace("Bathjroom", "a sink and a toilet", "in the shower");
            masterBedroom = new RoomWithHidingPlace("Master Bedroom", "a large bed", "under the bed");
            secondBedroom = new RoomWithHidingPlace("Second Bedroom", "a small bed", " under the bed");

            frontYard =new OutsideWithDoor("Front Yard", false, "a heavy-looking oak door ");
            backYard = new OutsideWithDoor("Back Yard", true, " a screen door");
            garden = new OutsideWithHidingPlace("Garden", false,"inside the shed");
            driveway = new OutsideWithHidingPlace("Driveway", true, "in the garage");

            diningRoom.Exits = new Location[] { livingRoom, kitchen };
            livingRoom.Exits = new Location[] { diningRoom, stairs };
            kitchen.Exits = new Location[] { diningRoom };
            stairs.Exits = new Location[] { livingRoom, hallway };
            hallway.Exits = new Location[] { stairs, bathroom, masterBedroom, secondBedroom };
            bathroom.Exits = new Location[] { hallway };
            masterBedroom.Exits = new Location[] { hallway };
            secondBedroom.Exits = new Location[] { hallway };
            frontYard.Exits = new Location[] { backYard, garden, driveway };
            backYard.Exits = new Location[] { frontYard, garden, driveway };
            garden.Exits = new Location[] { backYard, frontYard };
            driveway.Exits = new Location[] { backYard, frontYard };

            livingRoom.DoorLocation = frontYard;
            frontYard.DoorLocation = livingRoom;

            kitchen.DoorLocation = backYard;
            backYard.DoorLocation = kitchen;
        }
         private void ResetGame (bool displayMessage)
        {
            if (displayMessage)
            {
                MessageBox.Show("you found me in" + Moves + "moves!");
                IHidingPlace foundLocation = currentLocation as IHidingPlace;
                description.Text = "you found your opponent in " + Moves + "moves! He was hiding" + foundLocation.HidingPlaceName + ".";
            }
            Moves = 0;
            hide.Visible = true;
            goHere.Visible = false;
            check.Visible = false;
            goThroughTheDoor.Visible = false;
            exits.Visible = false;
        }

    

        private void goHere_Click(object sender, EventArgs e)
        {
            MoveToANewLocation(currentLocation.Exits[exits.SelectedIndex]);
        }

        private void goThroughTheDoor_Click(object sender, EventArgs e)
        {
            IHasExteriorDoor hasDoor = currentLocation as IHasExteriorDoor;
            MoveToANewLocation(hasDoor.DoorLocation);
        }

        private void check_Click(object sender, EventArgs e)
        {
            Moves++;
            if (opponent.Check(currentLocation))
                ResetGame(true);
            else
                RedrawForm();
        }

        private void Hide_Click(object sender, EventArgs e)
        {
            hide.Visible = false;
            for (int i = 1; i <= 10; i++)
            {
                opponent.Move();
                description.Text = i + "...";
                Application.DoEvents();
                System.Threading.Thread.Sleep(200);
            }
            description.Text = "Ready or not, here i come!";
            Application.DoEvents();
            System.Threading.Thread.Sleep(500);

            goHere.Visible = true;
            exits.Visible = true;
            MoveToANewLocation(livingRoom);
        }
    }

Although this program contains 0 errors the program stops compiling sometimes and points towards the line current.Exit.Name and give a pop up saying Null reference  exception was handled.

 

Here's how the form looks like FORM

Link to comment
https://www.neowin.net/forum/topic/1323070-an-error-occurring-during-compiling/
Share on other sites

7 answers to this question

Recommended Posts

  • 0
Location currentLocation = null;
// Or
Location currentLocation.Exits = null;

The error is caused because the value is null, this can be caused by either the whole currentLocation to be null or the Exits attribute to be null.

 

If I would have to guess:

 

You start at Location A which has exits B, C and D.

Then you go to exit Location C which has no exits, the attribute "exits" is null, this causes an error in the for loop which expects at least an empty array instead of null.

 

The NullReferenceException is the most common exception I've ever seen in a C# application :p

 

TL;DR

 

Always initialize your arrays/lists or check if they aren't null before using a loop.

 

Also this is not an compilation but an execution error, which means that the code will compile fine and can be distributed to the users which will see their application crash. Therefore it's always recommended to test all scenarios (0, 1 or multiple exits) for bugs before shipping the code.
 

This can be done manually or automated (recommended) with unit tests: https://www.youtube.com/watch?v=rW6LvPP4VvA

 

 

  • 0
  On 21/02/2017 at 12:51, Ch33f said:

Although this program contains 0 errors the program stops compiling sometimes and points towards the line current.Exit.Name and give a pop up saying Null reference  exception was handled.

Expand  

First, this is not a compile-time error! It does not happen while the program is compiling, it happens while the program is running, i.e. after compilation. If you have any confusion about compile- vs run-time you need to clear that up ASAP. The compiler turns your text files (*.cs) into executable files (*.dll, *.exe), which can then run inside an operating system process. Exceptions happen at run-time when your program is legal C# so it compiles, but it still manages to do something wrong (like in this case, dereferencing a null).

 

As @Seahorsepip mentions, if `currentLocation.Exits.Name` throws NullReferenceException then either currentLocation is null or currentLocation.Exits is null. Why are they null? What could you do to make them not null? That's yours to figure out.

 

 

  • Like 1
  • 0

After reading the code, it appears to me your problem is very simple...

 

abstract class Location
    {
        public Location(string name)
        {
            this.name = name;
        }
        public Location[] Exits;

you did not initialize Exits in the base constructor, nor did you initialize it in the inherited constructors as well.

Seems to me somewhere you should have this line somewhere in your constructors.

Exits = new Location[ExitCount];  //Where ExitCount is an integer

 

  • 0
  On 21/02/2017 at 18:19, Seahorsepip said:

The NullReferenceException is the most common exception I've ever seen in a C# application :p

Expand  

For me it's the "Object reference not set to an instance of an object" error (I've caused this in my apps forgetting to set it correctly), and the division by zero errors. I'm working on a C# GTA soundboard, and when I'm tired the object reference error happens a lot :) Caffeine normally helps :)

  • Like 1
  • 0
  On 21/02/2017 at 23:08, sao123 said:

After reading the code, it appears to me your problem is very simple...

Expand  

The exits are given a value in CreateObjects():

See Form1 constructor:

MoveToANewLocation(livingRoom);

Which sets currentLocation to livingRoom (which is generated by CreateObjects() and has the exits value diningRoom and stairs).

So exits don't need to be initialized in the Locations class itself to work but imagine what happens when the following method is called:

MoveToANewLocation(stairs);

And the stairs exits array attribute is not initialized in CreateObjects()?

 

Then it tries to for loop trough null instead of an array and we end up with a NullReferenceException :)

 

So initializing every array in the classes is a quick fix that does indeed solve the NullReferenceException issue in most cases but might not solve the underlying issue, maybe a Location should always have at least 1 exit as example.

 

As I said before it's saves a lot of work when unit tests are made for all use case scenarios(values, actions etc). Then it's directly clear when something stopped working that worked before.

 

  On 21/02/2017 at 23:33, Tidosho said:

For me it's the "Object reference not set to an instance of an object" error (I've caused this in my apps forgetting to set it correctly), and the division by zero errors. I'm working on a C# GTA soundboard, and when I'm tired the object reference error happens a lot :) Caffeine normally helps :)

Expand  

I always get StackOverflowExceptions since I have a habit to write a lot of recursion logic lately :p 

  • 0
  On 21/02/2017 at 23:33, Tidosho said:

For me it's the "Object reference not set to an instance of an object" error (I've caused this in my apps forgetting to set it correctly), and the division by zero errors. I'm working on a C# GTA soundboard, and when I'm tired the object reference error happens a lot :) Caffeine normally helps :)

Expand  

Try F# - no nulls (mostly) among other benefits ;)

  • 0

Thank You for the feedback, i got the program working, exits is the name given to the combo box and its has been initialized in Location class here is the edited version of form.

public partial class Form1 : Form
    {
        int Moves;
        Location currentLocation;

        RoomwithDoor livingRoom;
        RoomWithHidingPlace diningRoom;
        RoomwithDoor kitchen;
        Room stairs;
        RoomWithHidingPlace hallway;
        RoomWithHidingPlace bathroom;
        RoomWithHidingPlace masterBedroom;
        RoomWithHidingPlace secondBedroom;

        OutsideWithDoor frontYard;
        OutsideWithDoor backYard;
        OutsideWithHidingPlace garden;
        OutsideWithHidingPlace driveway;

        Opponent opponent;

        public Form1()
        {
            InitializeComponent();
            CreateObjects();
            opponent = new Opponent(frontYard);
            ResetGame(false);
            MoveToANewLocation(livingRoom);
        }
        private void MoveToANewLocation(Location newLocation)
        {
            Moves++;
            currentLocation = newLocation;
            RedrawForm();
            
        }
        private void RedrawForm()
        {
            exits.Items.Clear();
            for (int i = 0; i < currentLocation.Exits.Length; i++)
                exits.Items.Add(currentLocation.Exits[i].Name);
            exits.SelectedIndex = 0;
            description.Text = currentLocation.Description + "\r\n(move #" + Moves + ")";
            if(currentLocation is IHidingPlace)
            {
                IHidingPlace hidingPlace = currentLocation as IHidingPlace;
                check.Text = "Check" + hidingPlace.HidingPlaceName;
                check.Visible = true;
            }
            else
                check.Visible = false;
                if (currentLocation is IHasExteriorDoor)
                    goThroughTheDoor.Visible = true;
                else
                    goThroughTheDoor.Visible = false;
        }
        private void CreateObjects()
        {
            livingRoom = new RoomwithDoor("Living Room", "an antique carpet","inside the closet", "an oak door with a brassknob");
            diningRoom = new RoomWithHidingPlace("Dining Room", "a crystal chandelier", "in the tall armorie");
            kitchen = new RoomwithDoor("Kitchen", "stainless steel appliances","in the cabinet", "a screen door");
            stairs = new Room("Stairs", "a wooden bannister");
            hallway = new RoomWithHidingPlace("Upstairs Hallway", "a picture of a dog", "in the closet");
            bathroom = new RoomWithHidingPlace("Bathjroom", "a sink and a toilet", "in the shower");
            masterBedroom = new RoomWithHidingPlace("Master Bedroom", "a large bed", "under the bed");
            secondBedroom = new RoomWithHidingPlace("Second Bedroom", "a small bed", " under the bed");

            frontYard =new OutsideWithDoor("Front Yard", false, "a heavy-looking oak door ");
            backYard = new OutsideWithDoor("Back Yard", true, " a screen door");
            garden = new OutsideWithHidingPlace("Garden", false,"inside the shed");
            driveway = new OutsideWithHidingPlace("Driveway", true, "in the garage");

            diningRoom.Exits = new Location[] { livingRoom, kitchen };
            livingRoom.Exits = new Location[] { diningRoom, stairs };
            kitchen.Exits = new Location[] { diningRoom };
            stairs.Exits = new Location[] { livingRoom, hallway };
            hallway.Exits = new Location[] { stairs, bathroom, masterBedroom, secondBedroom };
            bathroom.Exits = new Location[] { hallway };
            masterBedroom.Exits = new Location[] { hallway };
            secondBedroom.Exits = new Location[] { hallway };
            frontYard.Exits = new Location[] { backYard, garden, driveway };
            backYard.Exits = new Location[] { frontYard, garden, driveway };
            garden.Exits = new Location[] { backYard, frontYard };
            driveway.Exits = new Location[] { backYard, frontYard };

            livingRoom.DoorLocation = frontYard;
            frontYard.DoorLocation = livingRoom;

            kitchen.DoorLocation = backYard;
            backYard.DoorLocation = kitchen;
        }
         private void ResetGame (bool displayMessage)
        {
            if (displayMessage)
            {
                MessageBox.Show("you found me in" + Moves + "moves!");
                IHidingPlace foundLocation = currentLocation as IHidingPlace;
                description.Text = "you found your opponent in " + Moves + "moves! He was hiding" + foundLocation.HidingPlaceName + ".";
            }
            Moves = 0;
            hide.Visible = true;
            goHere.Visible = false;
            check.Visible = false;
            goThroughTheDoor.Visible = false;
            exits.Visible = false;
        }

      /*  private void MoveToANewLocation(Location newLocation)
        {
            currentLocation = newLocation;

            exits.Items.Clear();
            for (int i = 0; i < currentLocation.Exits.Length; i++) 
            exits.Items.Add(currentLocation.Exits[i].Name);
            exits.SelectedIndex = 0;

            description.Text = currentLocation.Description;

            if (currentLocation is IHasExteriorDoor)
                goThroughTheDoor.Visible = true;
            else
                goThroughTheDoor.Visible = false;
        }*/

        private void goHere_Click(object sender, EventArgs e)
        {
            MoveToANewLocation(currentLocation.Exits[exits.SelectedIndex]);
        }

        private void goThroughTheDoor_Click(object sender, EventArgs e)
        {
            IHasExteriorDoor hasDoor = currentLocation as IHasExteriorDoor;
            MoveToANewLocation(hasDoor.DoorLocation);
        }

        private void check_Click(object sender, EventArgs e)
        {
            Moves++;
            if (opponent.Check(currentLocation))
                ResetGame(true);
            else
                RedrawForm();
        }

        private void Hide_Click(object sender, EventArgs e)
        {
            hide.Visible = false;
            for (int i = 1; i <= 10; i++)
            {
                opponent.Move();
                description.Text = i + "...";
                Application.DoEvents();
                System.Threading.Thread.Sleep(200);
            }
            description.Text = "Ready or not, here i come!";
            Application.DoEvents();
            System.Threading.Thread.Sleep(500);

            goHere.Visible = true;
            exits.Visible = true;
            MoveToANewLocation(livingRoom);
        }
    }

 

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Come the hell on, do we need clickbait titles? "Overwatch 2" - 11 characters "a popular multiplayer hero shooter" - 34 characters What's the purpose here - delivering news or titles for clicks? I think we all know the answer. You're straying into "Number 7 on this list will SHOCK you" territory and while it may work on some crappy sites, this is why they are crappy sites. Just tell the story! Go back and look at popular articles that made Neowin what it was. Did they have headlines of "New OS from major technology conglomerate has astonishing new feature" or is it likely to tell the story succinctly and then elaborate within the content?
    • You can now generate video clips with sound using Veo 3 in Google Vids by David Uzondu Last month, we reported that Google was adding its Veo 2 video generation model into the Google Vids editor. The company has now followed that by rolling out its latest Veo model, Veo 3, which was unveiled at I/O 2025 and went viral recently for its shockingly realistic clips. The main addition this time is the ability to generate both video and synchronized audio directly from a text prompt. Anyone with a compatible Google Workspace plan can now generate 8-second clips right inside a Vids project. You find the "Generate video" icon in the editor, pick Veo 3, and then type out what you want to see and hear. Google's examples include things like a spokesperson delivering a line for a product demo or an employee giving an intro to a safety video. The model is good enough to generate dialogue that actually syncs with the character’s lips. Once you create a clip, you can just insert it into your video. And, if the audio it spits out is garbage, you have the option to mute the clip. The generated clips are currently capped at 720p resolution and run at 24FPS, a far cry from the 4K potential Veo 3 has as a standalone model. Still, getting functional audio generation built-in is a huge step. This native sound capability is what really sets it apart from competitors like OpenAI's Sora, which primarily generates silent videos. Google began pushing out the feature recently, but do not worry if you do not see it immediately in your account. The company is doing a gradual rollout that can take up to 15 days to complete, so your access will depend on which wave your account is in and whether your company's admin uses the Rapid or Scheduled Release track. Access is fairly widespread for paying customers on Google Workspace, including those on Business Standard and Plus, as well as Enterprise Standard and Plus plans. It is also available for Essentials, Nonprofits, and even the lower-tier Business Starter and Enterprise Starter plans. For now, the AI generation only understands prompts in English, and all videos are invisibly watermarked to identify them as AI-generated.
    • My dear freedonX, have you thought about giving Linux a try? If you don't want to be nagged, that's surely the best place to go. And as long as you stick to something simple, you won't need to touch the command line once.
    • For windows, you have winget and chocolatey. For linux, you've got other package managers depending of the base, apt, dnf, zypper, pacman... I dunno about Mac, but even if it doesn't have an accessible package manager, there surely is a store, or worst case, http requests, curl, wget, or something similar.
  • Recent Achievements

    • First Post
      Kurotama earned a badge
      First Post
    • Collaborator
      Carltonbar earned a badge
      Collaborator
    • Explorer
      MusicLover2112 went up a rank
      Explorer
    • Dedicated
      MadMung0 earned a badge
      Dedicated
    • Rookie
      CHUNWEI went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      ATLien_0
      270
    3. 3
      +FloatingFatMan
      247
    4. 4
      +Edouard
      201
    5. 5
      snowy owl
      168
  • Tell a friend

    Love Neowin? Tell a friend!