簡體   English   中英

對象引用未設置為對象的實例(C#)

[英]Object reference not set to an instance of an object (C#)

我似乎無法正常工作,打開我的C#項目文件(.exe)后出現錯誤,並顯示“對象引用未設置為對象實例”。 這是線和下方的線。

// Update the actual position

Actor.Position = new Vector3(
                        Actor.PositionToSet.X, 
                        Actor.PositionToSet.Y, 
                        (Math.Round(
                            GetUserStepHeight(
                                Actor.PositionToSet),
                                1)));

Actor.PositionToSet = null;

可能是1. Actornull或2. Actor.PositionToSetnull

要進行檢查,可以使用以下方法:

if(Actor == null){
    System.Console.WriteLine("Actor is NULL");
}

if(Actor.PositionToSet == null){
    System.Console.WriteLine("Actor.PositionToSet is NULL");
}

或在Visual Studio中使用LINE BY LINE調試器。

這是我幾年前得到的一條建議:

  1. 在編寫代碼之前先三思,並建立代碼的思維模型,然后在事情破裂時,只需思考一下出了什么問題,而無需查看代碼。

  2. 對代碼進行單元測試,使其成為編碼方法的標准做法。 這將節省您數年的調試時間。

請檢查ActorActor.PositionToSet不為null。 那是唯一的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM