1

I have a simple line

if (currentClip.DynamicSpeed != null)

Where currentClip is my own datatype and DynamicSpeed is a custom type with few string and float variables.

In this case, currentClip is the object and says the DynamicSpeed is null. However, I still get a NullReferenceException from this line. Furthermore, it looks like the stack trace points to correct lines and all that in terms of where the exception comes from (some solutions suggested that they could be wrong and there were other solutions).

What could possibly cause this. There are no overloads to the != operator either.

[edit] Here are the relevant classes.

public class Clip2D
{
    public string Name;

    [ContentSerializer(Optional = true)]
    public bool FlipX;
    [ContentSerializer(Optional = true)]
    public bool FlipY;

    [ContentSerializer(Optional = true)]
    public string NextClip;

    [ContentSerializer(Optional = true)]
    public string PreviousClip;

    [ContentSerializer(Optional = true)]
    public DynamicSpeed DynamicSpeed;
}

public class DynamicSpeed
{
    public string AffectingVariable;

    public float MinSpeed;
    public float MaxSpeed;
    public float MinValue;
    public float MaxValue;
}

Should be noted that it doesn't ALWAYS throw it.

Muhwu
  • 1,151
  • 1
  • 10
  • 26

2 Answers2

3

Simple solution:

if (currentClip != null && currentClip.DynamicSpeed != null)

if currentClip is null, it wont check the second part of the conditional due to short circuiting.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
0

When it tries to evaluate

currentClip.DynamicSpeed

currentClip is likely to be null causing the exception.

Hoots
  • 1,876
  • 14
  • 23