我得到的錯誤,當我嘗試使用連接到它的BTN:C#「對象引用未設置爲對象的實例。」
private void btnAccel_Click(object sender, EventArgs e)
{
pStatus.Text = plane.speed.ToString();
plane.speed = double.Parse(txtSpeed.Text);
plane.Accelerate();
pStatus.Text = plane.speed.ToString();
}
pStatus是我使用的面板,並更新當前的速度之前和之後我提高速度。
Airplane plane = new Airplane();
的錯誤似乎當它到達plane.Accelerate();
public void Accelerate()
{
// increase the speed of the airplane
if (PlanePosition.speed < Position.MAX_SPEED)
{
PlanePosition.speed = PlanePosition.speed + 1; // or speed += 1;
}//end of if
numberCreated++; // increment the numberCreated each time an Airplane object is created
}//end of public Accelerate()
也就是說第一行if(PlanePosition.speed < Position.MAX_SPEED)
是它不斷從什麼VS告訴我發生的事情發生: plane
如上定義。
//private variables
private string name{get; set;}
private Position planePosition;
private static int numberCreated;
//default constructor
public Airplane()
{
}//end of public Airplane
public Position PlanePosition{get;set;}
class Position
{
//private variables
internal int x_coordinate;
internal int y_coordinate;
internal double speed;
internal int direction;
internal const int MAX_SPEED = 50;
//default constructor
public Position()
{
}//end of public Position
public string displayPosition()
{
return "okay";
}//end of public string displayPosition()
}//end of class Position
之前,您需要確保將對象分配給PlanePosition,好像您沒有初始化PlanePosition字段/屬性。 – siride 2011-01-20 16:01:52
您應該發佈PlanePosition和Position的代碼 – hackerhasid 2011-01-20 16:01:55
是PlanePosition還是定位對類的引用?如果是這樣,確保你已經實例化它,就像你的飛機類一樣。 – Bernard 2011-01-20 16:03:12