1
votes

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.

2
Have you checked to make sure currentClip is not also null? - JAB
Try if (currentClip != null && currentClip.DynamicSpeed != null) - Anthony Chu
The debugger shows currentClip is not null, it has it's other fields populated and DynamicSpeed is the only null field. - Muhwu
No getter for DynamicSpeed either. - Muhwu

2 Answers

3
votes

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.

0
votes

When it tries to evaluate

currentClip.DynamicSpeed

currentClip is likely to be null causing the exception.