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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝