更新节点时Null引用异常Neo4jClient . net

本文关键字:异常 Neo4jClient net 引用 Null 节点 更新 | 更新日期: 2023-09-27 18:13:57

我创建了一个方法,该方法接受节点属性并更新指定节点上的这些属性,但是当它得到代码进行更新时,我得到一个System.NullReferenceException: Object reference not set to an instance of an object.

代码如下:

public NodeReference<EntityNode> GraphUpdateEntityNode(
                        NodeReference<EntityNode> nodeId,
                        string guid,
                        string type,
                        string name,
                        string dateTimeCreated,
                        string currentVersionDateTimeCreated,
                        int versionCount,
                        int currentVersion)
{
    var nodeRef = (NodeReference<EntityNode>)nodeId;
    GraphOperations graphOp = new GraphOperations();
    graphOp.GraphGetConnection();
    clientConnection.Update(nodeRef, node =>
    {
        node.GUID = guid;
        node.Type = type;
        node.Name = name;
        node.CurrentVersion = currentVersion;
        node.DateTimeCreated = dateTimeCreated;
        node.CurrentVersionDateTimeCreated = currentVersionDateTimeCreated;
        node.VersionCount = versionCount;
    });
    return nodeRef.Id;
}

我在这里错过了什么?我是否必须通过执行var nodeRef = (NodeReference<EntityNode>)nodeId;再次获得节点的引用,因为我已经将其作为方法的参数传递进去了?在更新节点之前,我必须调用抽象的clientConnection.Connect吗?

下面是GraphGetConnection()方法:
GraphClient clientConnection;
public GraphClient GraphGetConnection()
        {
            GraphOperationsLogger.Trace("Entering GetConnection Method");
                clientConnection = new GraphClient(new Uri("http://localhost:7474/db/data"));
                clientConnection.Connect();
            return clientConnection;
        }

更新节点时Null引用异常Neo4jClient . net

看起来您正在实例化的clientConnection在另一个名为GraphOperations的类中
虽然您可能在该类中有一个具有相同名称的变量,但它不会由GraphOperations类分配。将您的代码更新为以下内容:

GraphOperations graphOp = new GraphOperations();
var clientConnection = graphOp.GraphGetConnection();

这将创建一个作用域变量,但是如果你想将它分配给这个类中的'变量',请执行以下操作,不使用var:

clientConnection = graphOp.GraphGetConnection();