drutjkpsr67393592 2016-08-06 19:10
浏览 61
已采纳

Golang:恐慌:运行时错误:使用bufio.Scanner无效的内存地址或nil指针取消引用

I am implementing a go program which uses bufio.Scanner and bufio.Writer i have packaged my code as follows

package main

import (
    "fmt"
    "player/command"
    "strings"
)

func main() {
    //Enter your code here. Read input from STDIN. Print output to STDOUT



    for commands.Scanner.Scan() {

        //scan a new line and send it to comand variable to check command exist or not
        input := strings.Split(strings.Trim(commands.Scanner.Text(), " "), " ")
        command := input[0]

        if command == "" {
            fmt.Printf("$ %s:", commands.Pwd)
            continue
        }

        if !commands.Commands[command] {
            commands.ThrowError("CANNOT RECOGNIZE INPUT.")

        } else {

            commands.Execute(command, input[1:], nil)

        }
        fmt.Printf("$ %s:", commands.Pwd)
    }
}

I am also using init.go file in main package as follows

package main

import (
    "flag"
    "player/source"
)

func init() {
    sourceFlag := flag.String("filename", "", "if input is through source file")
    flag.Parse()
    if *sourceFlag != "" {
        source.Input(*sourceFlag)
    }
}

and my final package player/source is as follows :-

package source

    import (
        "bufio"
        "log"
        "os"
        "player/command"
    )

    func Input(source string) {
        if source != "" {
            readFile, err := os.OpenFile(source, os.O_RDONLY, os.ModeExclusive)
            if err != nil {
                log.Fatal(err)
            }
            commands.Scanner = bufio.NewScanner(readFile)
            writeFile, err := os.Create(source + "_output.txt")
            if err != nil {
                log.Fatal(err)
            }
            commands.Writer = bufio.NewWriter(writeFile)
        } else {
            commands.Scanner = bufio.NewScanner(os.Stdin)
            commands.Writer = bufio.NewWriter(os.Stdout)
            // fmt.Println(commands.Scanner)
        }
    }

Execution of this code results in

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x58 pc=0x4a253a]

goroutine 1 [running]:
bufio.(*Scanner).Scan(0x0, 0x5)
    /usr/local/go/src/bufio/scan.go:120 +0x2a
main.main()
    /home/xyz/dev/go/src/players/main.go:13 +0x124

I dont know the reason even after initializing my scanner why i am not been able to read from it

  • 写回答

1条回答 默认 最新

  • doufei4418 2016-08-06 19:35
    关注

    One reason why command.Scanner is not initialized could be that you are not passing a filename argument to your main script. In this case, source.Input(*sourceFlag) is never called, as per the if condition (if *sourceFlag != "" is false in case of a missing filename option).

    Also, since you are checking for an empty file name later in source, this condition in main's init is redundant. Try:

    func init() {
        sourceFlag := flag.String("filename", "", "if input is through source file")
        flag.Parse()
        source.Input(*sourceFlag)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度