Hello can someone please explain me how can one object pass through a NULL
guard then throws exception when trying to acess non-nullable field? (bool)
As you can see in the method above our object response
of type Node
is not null.Focus your attention on the boolean
field IsArray
!!
Lets see what happens when our response
enters the Deserialize
method:
Passes null guard !!
Then crashes with Null Reference Exception
on an non-nullable field
.Exactly on the field i said to focus: the boolean
IsArray
.
Stack Trace
{System.NullReferenceException: Object reference not set to an instance of an object.
at redius.Node.get_Kind() in D:\Work\redius\core\redius\Internals\Resp\Node.cs:line 15
at redius.Node.get_IsArray() in D:\Work\redius\core\redius\Internals\Resp\Node.cs:line 22
at redius.Deserializer.List.Deserialize(Node node) in D:\Work\redius\core\redius\Internals\Formatter\Deserializer\Deserializer.List.cs:line 19}
Can someone please explain me how is this possible?Below is the Node
type:
public abstract partial class Node : IEquatable<Node> {
public enum Discriminator {
String,
Integer,
Array
}
public Discriminator Kind => this.NodeKind;
protected abstract Discriminator NodeKind { get; }
public Node.Array AsArray => this as Node.Array;
public Node.Integer AsInteger => this as Node.Integer;
public Node.String AsString => this as String;
public bool IsArray => this.Kind == Discriminator.Array;
public bool IsString => this.Kind == Discriminator.Integer;
public bool IsInteger => this.Kind == Discriminator.String;
#region IRAW
protected virtual ReadOnlyMemory<byte> GetRaw() {
return new byte[] { };
}
#endregion
public bool Equals(Node other) {
if (this.Kind != other.Kind) {
return false;
}
bool result;
switch (other.Kind) {
case Discriminator.Array: result = this.AsArray.Equals(other.AsArray); break;
case Discriminator.Integer: result = this.AsInteger.Equals(other.AsInteger); break;
case Discriminator.String: result = this.AsString.Equals(other.AsString); break;
default: throw new NotImplementedException();
}
return result;
}
}
P.S: When the Node
enters the Deserialize
method something goes wrong with the debugger since i can't acess it (can not acess its fields) !
P.S 2 If i try to add the following line in the Deserializer
method :
var values = node.GetType().GetFields().Select(x => x.GetValue(node));
the application enters a break state and i get the following exception :
System Execution Engine Exception