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.

5
  • 8
    Have you checked to make sure currentClip is not also null? Commented Apr 29, 2014 at 16:19
  • Try if (currentClip != null && currentClip.DynamicSpeed != null) Commented Apr 29, 2014 at 16:20
  • The debugger shows currentClip is not null, it has it's other fields populated and DynamicSpeed is the only null field. Commented Apr 29, 2014 at 16:20
  • No getter for DynamicSpeed either. Commented Apr 29, 2014 at 19:55
  • Possible duplicate of What is a NullReferenceException and how do I fix it? Commented Oct 4, 2015 at 9:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you post the class layout for currentclip?
Posted to original post.
0

When it tries to evaluate

currentClip.DynamicSpeed

currentClip is likely to be null causing the exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.