-
Notifications
You must be signed in to change notification settings - Fork 2.3k
*sql.DB invalid memory address or nil pointer dereference #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We need the following information:
|
go version go1.1.2 darwin/amd64 |
I think the panic shouldn't occur with v1.0.3, which is a bugfix release we released yesterday ( But I'm investigating further, the stacktrace looks really strange to me. The panic is caused by a v1.0.3 can at least prevent the nil-pointer panic, since it checks |
Can you reproduce this error? |
/Users/jinke/golang/src/cds_spider/newCar/koubei/log.go func AddOk(source string, count int) error {
row := dbpool.DB.QueryRow("SELECT * FROM cheduoshao_spider_status.koubei WHERE source=? AND spider_date=?", source, gcode.Date())
var (
id, quantity int
sources, spider_date string
)
row.Scan(&id, &sources, &quantity, &spider_date)
if id == 0 {
stmt, err := dbpool.DB.Prepare("INSERT cheduoshao_spider_status.koubei SET source=?, quantity=?, spider_date=?")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(source, 1, gcode.Date())
if err != nil {
return err
}
} else {
dbpool.DB.Exec("UPDATE main.koubei SET quantity=? WHERE id=?", quantity+1, id)
}
return nil
} dbpool.DB.Exec("UPDATE main.koubei SET quantity=? WHERE id=?", quantity+1, id) IS ERROR |
Seem to have not found such a situation before, my system is OS X 10.9 |
Check if |
I tried a few times more, I just delete the old version, go get it again
|
I think I was able to reproduce this bug. Once again, this bug is related to https://codereview.appspot.com/14920046 / Go issue 5718 (or #98, #142). v1.0.3 includes #143, which at least avoids the panics. v1.1, which will be released in a few hours, will include #151, which improves this a bit more. I'd suggest to either manually patch your Go version with https://codereview.appspot.com/14920046 or avoid And a note to your code above: Here is how I would modify your code above: func AddOk(source string, count int) error {
var (
id, quantity int
sources, spider_date string
)
if err := dbpool.DB.QueryRow("SELECT * FROM cheduoshao_spider_status.koubei WHERE source=? AND spider_date=?", source, gcode.Date()).Scan(&id, &sources, &quantity, &spider_date); err != nil {
return err
}
if id == 0 {
if _, err =: dbpool.DB.Exec("INSERT cheduoshao_spider_status.koubei SET source=?, quantity=?, spider_date=?", source, 1, gcode.Date()); err != nil {
return err
}
} else {
if _, err =: dbpool.DB.Exec("UPDATE main.koubei SET quantity=? WHERE id=?", quantity+1, id); err != nil {
return err
}
}
return nil
} |
Ok,Thanks! |
I'm having too this problem, go version is
This is the function _getJSON, that returns a JSON-Encoded string of the results:
|
@danilopolani Your db is nil. |
Why should it be nil? I have a db var outside the functions
Then it is called in |
Then, db is local variable of main, not global variable Without you provide complete reproducible code, no one can point out |
@methane, you were right, I had to change |
Just ran into same issue. Surprised that this was issue. |
@danilopolani @methane you guys saved my time |
@danilopolani You are right, thank you very much for your help, I have been searching for this problem for a long time, thank you |
running into same issue.. how do i use @danilopolani solution? |
Thanks @methane @danilopolani I made the same mistake, luckily I found this discussion. |
OMG, I could have never imagined the solution was about variables scoping. |
*sql.DB invalid memory address or nil pointer dereference Source solution: go-sql-driver/mysql#150 (comment)
2021 and you saved my day too! |
@methane |
more over 2024, the same story... |
Sep 2024, same here 🤦 |
//The file /Users/jinke/golang/src/cds_spider/common/dbpool/mysql.go
//The file /Users/jinke/golang/src/cds_spider/newCar/koubei/koubei.go
"stmt, err := dbpool.DB.Prepare" is Error invalid memory address or nil pointer dereference
The text was updated successfully, but these errors were encountered: