views:

66

answers:

2

In this class, I am setting elp to ElType in the constructor.

I can access properties of elp fine when in the constructor (the // ... bit is where I'm accessing elp's properties), but when I try to access elp in another method - ucp() - my program crashes with NullReferenceException.

I can't figure out what I'm doing wrong here, although it would probably be something pretty straight forward.

[Serializable]
public class ElBase : RectangleNode
{
    public ElementParameters elp;

    public ElBase(ElementParameters ElType)
    {
        this.elp = ElType;

        // ...
    }

    private void ucp()
    {
        int i = 0;

        if (this.elp.HasInput)
        {
            // ...
        }
    }
+1  A: 

Can't say much without viewing the calling code but looks like ElementParameters passed to constructor was either NULL or not initialized.

Aamir
+5  A: 

Either:

  1. elp is set to null, possibly even in the constructor if there's not a null check there
  2. The HasInput property getter is throwing the NullReferenceException based on code inside it.
  3. Oh dear: elp is a publicly accessible field. Anything can set it to null. :o This should be number 1, but I looked straight past it since no one makes publicly accessible fields, hence it's never a problem.
280Z28