rocketlaunchr / dataframe-go

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration
Other
1.19k stars 95 forks source link

panic: runtime error: invalid memory address or nil pointer dereference #21

Closed kaviarasankk closed 4 years ago

kaviarasankk commented 4 years ago

content, err := ioutil.ReadFile(parsedDirectory, file.Name()) if err != nil{ fmt.Println(err) return } df, err := imports.LoadFromCSV(ctx, bytes.NewReader(content)) var writers io.Writer jsonErr := exports.ExportToJSON(ctx, writers, df) if jsonErr != nil{ fmt.Println("Json export error") }

The above script throws below error

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4e9f01]

goroutine 1 [running]: encoding/json.(*Encoder).Encode(0xc000045e00, 0x5b83c0, 0xc0000601e0, 0x0, 0xc0002f22e8) /usr/local/go/src/encoding/json/stream.go:231 +0x1b1 github.com/rocketlaunchr/dataframe-go/exports.ExportToJSON(0x62a220, 0xc000016080, 0x0, 0x0, 0xc00001a080, 0x0, 0x0, 0x0, 0x0, 0x0) /go/pkg/mod/github.com/rocketlaunchr/dataframe-go@v0.0.0-20200305003105-0059c0c62d69/exports/jsonl.go:83 +0x395

pjebs commented 4 years ago

@propersam can you look at this

pjebs commented 4 years ago

@kaviarasankk

After this line: df, err := imports.LoadFromCSV(ctx, bytes.NewReader(content)) Can you check err. If err != nil, then don't proceed.

pjebs commented 4 years ago

This line is also wrong:

var writers io.Writer
jsonErr := exports.ExportToJSON(ctx, writers, df)

writers is nil. It needs to actually be something.

kaviarasankk commented 4 years ago
           `content, err := coreio.ReadFile(parsedDirectory, file.Name())
    if err != nil{
        fmt.Println(err)
        return
    }

    df, err := imports.LoadFromCSV(ctx, bytes.NewReader(content))
    if err != nil{
        fmt.Println(err)
        log.Fatal(err)
    }

    var writers io.Writer
    jsonErr := exports.ExportToJSON(ctx, writers, df)
    if jsonErr != nil{
        fmt.Println("Json export error")
    }`

Tried the above code still the issue persist

pjebs commented 4 years ago

writers is nil. That's why it's panicing.

propersam commented 4 years ago

Hi @kaviarasankk you need to implement Writer properly..

update: when you use the ExportToJSON method..you are expected to pass a pointer containing file location that the result will be written to..and that is why your 'writer' is supposed to be a pointer to an open file. but instead your writer is empty. Hence the error. so io.writer is not properly implemented .. or you can try this other method i use..if you like..(if it makes sense in your context)

import "os"

// create/open a file using os.OpenFile
file, err := os.OpenFile("result.json", os.O_CREATE|os.O_WRONLY, 0777) // creates the file if it doesn't exist
if err != nil {
    fmt.Println(err)
}
defer file.Close()

// Then pass your 'file' into the ExportToJSON method
if err := exports.ExportToJSON(ctx, file, df); err != nil {
    fmt.Println(err)
}