-
Notifications
You must be signed in to change notification settings - Fork 199
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
Comments
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 |
A known issue, Edit: or @henon 's suggestion which works well too. |
@Plankton555 Try to use |
@Oceania2018 Will try that. My current work-around is to initialize to |
Yes, |
If it is a known issue (to quote Nucs), shouldn't it still be fixed? (regarding closing this ticket) |
@Plankton555, It is a known issue and its fix is complicated that'll require breaking changes. This is why it has been delayed. I'll keep it open till we either document the proper use ( |
I've added a unit-test and calling |
Summary
It doesn't seem to be possible to use
if (myArray == null)
to check whether myArray is null, if it has been initialized toNDArray myArray = null;
. This is because the == operator has been overridden without any additional checks.Minimal example of bug
Expected behaviour
myArray == null
should return true, not throw a NullReferenceException.The text was updated successfully, but these errors were encountered: