简体   繁体   中英

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

I can't seem to get this working, I'm getting an error once opening my c# project file (.exe) and it says "object reference not set to an instance of an object". Here's the line and the one below it.

// Update the actual position

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

Actor.PositionToSet = null;

The possibility is, either 1. Actor is null or 2. Actor.PositionToSet is null .

To check, either have this:

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

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

or use the LINE BY LINE debugger in Visual Studio.

Here is a piece of advice I got years ago:

  1. Think before you code, and build a mental model of the code, then when things break, just reflect on what went wrong without looking at the code.

  2. Unit test your code, make it a standard practice of your coding methodology. This will save you years of debugging time.

Please check that Actor or Actor.PositionToSet is not null. That is the only reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM