Skip to content

Instantly share code, notes, and snippets.

@garrensmith
Created February 11, 2013 07:23
Show Gist options
  • Select an option

  • Save garrensmith/4753114 to your computer and use it in GitHub Desktop.

Select an option

Save garrensmith/4753114 to your computer and use it in GitHub Desktop.
Sql invalid memory address or nil pointer deference error
ab -c 35 -n 1000 http://127.0.0.1:8000/
http: panic serving 127.0.0.1:51751: runtime error: invalid memory address or nil pointer dereference
/usr/local/Cellar/go/1.0.3/src/pkg/net/http/server.go:589 (0x342dc)
_func_004: buf.Write(debug.Stack())
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/proc.c:1443 (0x11689)
panic: reflect·call(d->fn, d->args, d->siz);
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/runtime.c:128 (0x12155)
panicstring: runtime·panic(err);
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/thread_darwin.c:418 (0x152a4)
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
/usr/local/Cellar/go/1.0.3/src/pkg/database/sql/sql.go:1004 (0x62f12)
(*Rows).Close: if rs.closed {
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/proc.c:1443 (0x11689)
panic: reflect·call(d->fn, d->args, d->siz);
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/runtime.c:128 (0x12155)
panicstring: runtime·panic(err);
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/thread_darwin.c:418 (0x152a4)
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
/usr/local/Cellar/go/1.0.3/src/pkg/database/sql/sql.go:911 (0x62449)
(*Rows).Next: if rs.closed {
/Users/garrensmith/WebDev/go/sql_server.go:42 (0x234b)
GetAll: for rows.Next() {
/Users/garrensmith/WebDev/go/sql_server.go:66 (0x28b3)
boom: orgs, _ := GetAll()
/usr/local/Cellar/go/1.0.3/src/pkg/net/http/server.go:703 (0x28125)
HandlerFunc.ServeHTTP: f(w, r)
/usr/local/Cellar/go/1.0.3/src/pkg/net/http/server.go:941 (0x28f79)
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/local/Cellar/go/1.0.3/src/pkg/net/http/server.go:669 (0x27f38)
(*conn).serve: handler.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/proc.c:271 (0xf78f)
goexit: runtime·goexit(void)
package main
import (
"os"
"net/http"
"log"
"fmt"
"time"
"encoding/json"
"database/sql"
_ "github.com/bmizerany/pq"
)
type Organisation struct {
Id int `json:"id"`
Name string `json:"name"`
Subdomain string `json:"subdomain"`
UserCount int `json:"users"`
}
var (
db *sql.DB
)
func newConn(conn string) {
var err error
log.Println("connection")
log.Println(conn)
db, err = sql.Open("postgres", conn)
if err != nil {
fmt.Printf("Another ERROR %+v", err)
}
}
func GetAll() ([]Organisation, error) {
organisations := make([]Organisation, 0)
rows, err := db.Query("SELECT id, name, subdomain FROM organisations")
defer rows.Close()
for rows.Next() {
org := Organisation{}
rows.Scan(&org.Id, &org.Name, &org.Subdomain)
org.UserCount = getUserCount(db, org.Id)
organisations = append(organisations, org)
}
return organisations, err
}
func getUserCount(db *sql.DB, id int) (count int) {
row := db.QueryRow("SELECT COUNT(*) FROM organisation_users WHERE organisation_users.organisation_id = $1", id)
row.Scan(&count)
return
}
func boom(w http.ResponseWriter, req *http.Request) {
w.Header().Add("content-type", "application/json")
fmt.Println("boom request", time.Now())
orgs, _ := GetAll()
orgs_json, _ := json.Marshal(orgs)
w.Write(orgs_json)
}
func main() {
fmt.Println("Go Server running on 8000 \n")
newConn(os.Getenv("DATABASE_URL"))
http.HandleFunc("/", boom)
err := http.ListenAndServe(":8000", nil )
if err != nil {
log.Printf("error running docs webserver: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment