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
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 classForm1:Form{intMoves;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;publicForm1(){InitializeComponent();CreateObjects();
opponent =newOpponent(frontYard);ResetGame(false);MoveToANewLocation(livingRoom);}privatevoidMoveToANewLocation(Location newLocation){Moves++;
currentLocation = newLocation;RedrawForm();}privatevoidRedrawForm(){
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;}privatevoidCreateObjects(){
livingRoom =newRoomwithDoor("Living Room","an antique carpet","inside the closet","an oak door with a brassknob");
diningRoom =newRoomWithHidingPlace("Dining Room","a crystal chandelier","in the tall armorie");
kitchen =newRoomwithDoor("Kitchen","stainless steel appliances","in the cabinet","a screen door");
stairs =newRoomwithDoor("Kitchen","stainless steel appliances","in teh cabinet","a screen door");
hallway =newRoomWithHidingPlace("Upstairs Hallway","a picture of a dog","in the closet");
bathroom =newRoomWithHidingPlace("Bathjroom","a sink and a toilet","in the shower");
masterBedroom =newRoomWithHidingPlace("Master Bedroom","a large bed","under the bed");
secondBedroom =newRoomWithHidingPlace("Second Bedroom","a small bed"," under the bed");
frontYard =newOutsideWithDoor("Front Yard",false,"a heavy-looking oak door ");
backYard =newOutsideWithDoor("Back Yard",true," a screen door");
garden =newOutsideWithHidingPlace("Garden",false,"inside the shed");
driveway =newOutsideWithHidingPlace("Driveway",true,"in the garage");
diningRoom.Exits=newLocation[]{ livingRoom, kitchen };
livingRoom.Exits=newLocation[]{ diningRoom, stairs };
kitchen.Exits=newLocation[]{ diningRoom };
stairs.Exits=newLocation[]{ livingRoom, hallway };
hallway.Exits=newLocation[]{ stairs, bathroom, masterBedroom, secondBedroom };
bathroom.Exits=newLocation[]{ hallway };
masterBedroom.Exits=newLocation[]{ hallway };
secondBedroom.Exits=newLocation[]{ hallway };
frontYard.Exits=newLocation[]{ backYard, garden, driveway };
backYard.Exits=newLocation[]{ frontYard, garden, driveway };
garden.Exits=newLocation[]{ backYard, frontYard };
driveway.Exits=newLocation[]{ backYard, frontYard };
livingRoom.DoorLocation= frontYard;
frontYard.DoorLocation= livingRoom;
kitchen.DoorLocation= backYard;
backYard.DoorLocation= kitchen;}privatevoidResetGame(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;}privatevoid goHere_Click(object sender,EventArgs e){MoveToANewLocation(currentLocation.Exits[exits.SelectedIndex]);}privatevoid goThroughTheDoor_Click(object sender,EventArgs e){IHasExteriorDoor hasDoor = currentLocation as IHasExteriorDoor;MoveToANewLocation(hasDoor.DoorLocation);}privatevoid check_Click(object sender,EventArgs e){Moves++;if(opponent.Check(currentLocation))ResetGame(true);elseRedrawForm();}privatevoidHide_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.
This is the first thing I do on a clean installation of Windows and on Android I also enable the developer options and make the animations faster. Can't find anything in iOS because of course, Apple...
Question
Ch33f
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
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 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; } } }
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