8000 NullReferenceException when checking if NDArray is null · Issue #290 · SciSharp/NumSharp · GitHub
[go: up one dir, main page]

Skip to content

NullReferenceException when checking if NDArray is null #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Plankton555 opened this issue Jun 25, 2019 · 8 comments
Closed

NullReferenceException when checking if NDArray is null #290

Plankton555 opened this issue Jun 25, 2019 · 8 comments
Labels
bug Something isn't working

Comments

@Plankton555
Copy link
Contributor

Summary
It doesn't seem to be possible to use if (myArray == null) to check whether myArray is null, if it has been initialized to NDArray myArray = null;. This is because the == operator has been overridden without any additional checks.

Minimal example of bug

static void Main(string[] args)
{
    NDArray myArray = null;

    if (myArray == null) // NullReferenceException is thrown here
    {
        Debug.WriteLine("myArray is null");
    }
}

Expected behaviour
myArray == null should return true, not throw a NullReferenceException.

@henon
Copy link
Contributor
henon commented Jun 25, 2019

I recently had similar problems with my equality operator implementations in Numpy.NET and Torch.NET. Here is a very good solution for comparison: You need to use object.ReferenceEquals to check against null.

        public static bool operator ==(NDArray a, object b)
        {
            if (ReferenceEquals(a, b))
                return true;
            if (ReferenceEquals(a, null))
                return false;
            if (ReferenceEquals(b, null))
                return false;
            return a.Equals(b);
        }

        public static bool operator !=(NDArray a, object b)
        {
            if (ReferenceEquals(a, b))
                return false;
            if (ReferenceEquals(a, null))
                return true;
            if (ReferenceEquals(b, null))
                return true;
            return !a.Equals(b);
        }

    

The comparison logic for NDArrays that are not the same reference should reside in the Equals method

@Nucs
Copy link
Member
Nucs commented Jun 25, 2019

A known issue,
Use object.Equals(myArray, null) to perform such checks.

Edit: or @henon 's suggestion which works well too.

@Nucs Nucs added the bug Something isn't working label Jun 25, 2019
@Oceania2018
Copy link
Member

@Plankton555 Try to use if (myArray is null).

@Plankton555
Copy link
Contributor Author

@Oceania2018 Will try that.

My current work-around is to initialize to NDArray myArray = new NDArray(typeof(float[])); and check for if (measurementMemory.len == 0), but I want to change that to a less ugly solution asap :P

@Plankton555
Copy link
Contributor Author

Yes, if (myArray is null) works as expected. Thanks for the work-around, @Oceania2018!

@Plankton555
Copy link
Contributor Author

If it is a known issue (to quote Nucs), shouldn't it still be fixed? (regarding closing this ticket)

@Nucs
Copy link
Member
Nucs commented Jun 26, 2019

@Plankton555, It is a known issue and its fix is complicated that'll require breaking changes. This is why it has been delayed.
It happens (as far as I recall) because of the implicit casting mechansim in NDArray.

I'll keep it open till we either document the proper use (myArray is null) or fix it.

@Nucs Nucs reopened this Jun 26, 2019
Nucs added a commit that referenced this issue Aug 6, 2019
@Nucs
Copy link
Member
Nucs commented Aug 6, 2019

I've added a unit-test and calling (ndarray==null) works as expected.

@Nucs Nucs closed this as completed Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants
0