douguluan5102 2019-01-29 07:35
浏览 58
已采纳

错误:紧急:运行时错误:无效的内存地址或nil指针取消引用。 在另一个结构中存在的结构中更改映射,

I have to structs lets say struct1 and struct2, struct2 contains a map with struct1, struct1 also contains a map, I want to change the map which is present in struct1.

This is throwing a runtime error: panic: runtime error: invalid memory address or nil pointer dereference

type FailureData struct {
    failuresInCommits map[string][]string 
}

type DetectionResults struct {
    Failures map[git_repo.FilePath]*FailureData
        //Have other things
}

func (r *DetectionResults) Fail(filePath git_repo.FilePath, message 
           string, commits []string) {  
        ok := r.Failures[filePath].failuresInCommits //error occurs here    
        if r.Failures[filePath].failuresInCommits == nil {       
            r.Failures[filePath].failuresInCommits = make(map[string][]string)
        }
        if len(ok) == 0 {
            r.Failures[filePath].failuresInCommits[message] = commits
        } else {
            r.Failures[filePath].failuresInCommits[message] = 
               append(r.Failures[filePath].failuresInCommits[message], 
                      commits...)   
        }
}
  • 写回答

1条回答 默认 最新

  • douzhui8531 2019-01-29 07:44
    关注

    The code you write will not pop nil error while compiling.It only causes nil point error when you are using it in a wrong way.

    failuresInCommits map[string][]string did you use it after make()?

    Failures map[git_repo.FilePath]*FailureData did you use this after 'make()'?

    ok , now you focus on ok := r.Failures[filePath].failuresInCommits,did you make sure r.Failures[filePath] returns 'failuresIncommits, true',

    if not, then r.Failures[filePath] is nil, ok, you tell me what is nil.failuresInCommits.

    there is also a risk that you can only x.failureInCommits in this specific package. If you do the same in some other package, x.failureInCommits will be unaccessable becaues of the field lowwer case limited.

    How to make ?

    package main
    
    type FilePath string
    
    type FailureData struct {
        failuresInCommits map[string][]string
    }
    
    func NewFailureData() FailureData {
        return FailureData{
            failuresInCommits: make(map[string][]string, 0),
        }
    }
    func (fd *FailureData) Set(k string, v []string) {
        fd.failuresInCommits[k] = v
    }
    
    type DetectionResults struct {
        Failures map[FilePath]*FailureData
        //Have other things
    }
    
    func NewDetectionResults() *DetectionResults {
        return &DetectionResults{
            Failures: make(map[FilePath]*FailureData, 0),
        }
    }
    func (dr *DetectionResults) Set(fp FilePath, fd *FailureData) {
        dr.Failures[fp] = fd
    }
    
    func main() {
        fd := NewFailureData()
        dr := NewDetectionResults()
        comments := []string{"push error", "commit error", "add error"}
    
        fd.Set("hash-18ef8abujce8fad0h8j", comments)
        dr.Set("/etc/go/gopath/src/github.com/xxx/xxx/main.go: 12", &fd)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)