dqqyp90576 2014-12-10 16:11
浏览 42
已采纳

Golang / neoism:查询返回的对象的调用方法导致恐慌

When trying to call a method of a Node object returned by a call to neoism.CypherQuery, I keep getting "invalid memory address or nil pointer dereference" panics. The query returns something (accessing the Data property of the Node works), but calling any method leads to a panic. The methods have the receiver *Node, not Node, but AFAIK that should work nevertheless? Anyway, I have already tried getting a pointer to the object and calling the method on that, but that didn't work either. I'm really stuck here...

Example code to reproduce the problem (needs neoism and go-uuid packages and a Neo4J DB running on localhost):

package main

import (
    "code.google.com/p/go-uuid/uuid"
    "fmt"
    "github.com/jmcvetta/neoism"
)

func main() {
    neo, _ := neoism.Connect("http://localhost:7474/db/data")

    // create a node with a random id
    nodeId := uuid.New()
    _, err := neo.CreateNode(neoism.Props{"NodeId": nodeId})
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("node created, id", nodeId)

    // find the node by the id
    res := []struct {
        Node neoism.Node `json:"nodes"`
    }{}
    err = neo.Cypher(&neoism.CypherQuery{
        Statement:  `MATCH (nodes {NodeId:{NodeId}}) RETURN nodes`,
        Parameters: neoism.Props{"NodeId": nodeId},
        Result:     &res,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("query executed")

    // try to work with the query results
    if len(res) > 0 {
        // get Data -> works
        fmt.Println(res[0].Node.Data)
        // call method -> panics
        err = res[0].Node.SetProperty("TestProp", "TestValue")
        if err != nil {
            fmt.Println(err)
            return
        }
    }
}

Here's the relevant part of the stack trace:

goroutine 1 [running]:
github.com/jmcvetta/neoism.(*entity).SetProperty(0x119abc00, 0x5d3a68, 0x8, 0x5d3a88, 0x9, ...)
        .../src/github.com/jmcvetta/neoism/entity.go:26 +0x104
main.main()
        .../src/nieware/neoprob/neoprob.go:41 +0x4cb
  • 写回答

1条回答 默认 最新

  • duan41497 2014-12-10 19:00
    关注

    Looking at the source for the SetProperty method: https://github.com/jmcvetta/neoism/blob/master/entity.go#L22

    It looks like it comes from an embedded struct and is not actually a method on the Node struct. The embedded entity struct is not a pointer however so it also should not be null.

    The stack trace shows that the panic occurs at line 26, because e.Db is not initialized:

    resp, err := e.Db.Session.Put(url, &value, nil, &ne)
    

    setting e.Db before calling SetProperty solves the problem:

    res[0].Node.Db = neo
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道ban了很多东西的pyjail题
  • ¥15 C++识别堆叠物体异常
  • ¥15 微软硬件驱动认证账号申请
  • ¥15 有人知道怎么在R语言里下载Git上的miceco这个包吗
  • ¥15 GPT写作提示指令词
  • ¥20 根据动态演化博弈支付矩阵完成复制动态方程求解和演化相图分析等
  • ¥20 关于DAC输出1.000V对分辨率和精度的要求
  • ¥15 华为超融合部署环境下RedHat虚拟机分区扩容问题
  • ¥15 哪位能做百度地图导航触点播报?
  • ¥15 请问GPT语言模型怎么训练?