GO SDK Couchbase Bucket connection Error- runtime error: invalid memory address or nil pointer dereference

connections

#1

Hi,

I created a go utility through which I am able connect to the Couchbase server but opening the bucket throws a runtime error saying:

ERROR OPEN BUCKET: failed to connect to any of the specified hosts
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x50 pc=0x6abe7e]

goroutine 1 [running]:
gopkg.in/couchbase/gocb.v1.(*Bucket).Do(0x0, 0xc0422e0000, 0x3e8, 0x400, 0x95bb60, 0x98f570)
E:/Google GO Projects/src/gopkg.in/couchbase/gocb.v1/bucket_multi.go:31 +0x3e
main.InsertData(0x0, 0xc042353ec0)
E:/Google GO Projects/src/CBPost.go:67 +0x5b
main.main()
E:/Google GO Projects/src/CBPost.go:34 +0x41e

The code is :

package main
import (
      "database/sql"
      "log"
      "fmt"
      _ "github.com/denisenkom/go-mssqldb"
      "gopkg.in/couchbase/gocb.v1"
      )

func main() {


	cluster, err1 := gocb.Connect("couchbase://localhost")
	if err1 = nil {
	   fmt.Println("Connected to COUCHBASE SERVER:localhost", err1)
	   }
	else if err1 != nil {
	   fmt.Println("ERROR CONNECT THE COUCHBASE SERVER:", err1)
	   }
	bucket, err2 := cluster.OpenBucket("example", "********")
	if err2 = nil {
	   fmt.Println("CONNECTED TO BUCKET :localhost", err2)
	   }
	else if err2 != nil {
	   fmt.Println("ERROR OPEN BUCKET:", err2)
	   }
	condb, err3 := sql.Open("mssql", "server=.\\SQL16;port=62587; user id=sa;password=****;")
		if err3 != nil {
	   fmt.Println("ERROR CONNECTING SQL SERVER:", err3)
	   }

		getData(condb, &items)											//Get data From SQL Server
		InsertData(bucket, &items)											// Bulk Load JSON into Couchbase Data Bucket
	
	err := bucket.Close()
	if err != nil {
	   fmt.Println("ERROR CLOSING COUCHBASE CONNECTION:", err)
	   }
		
	err = condb.Close()
	if err != nil {
	   fmt.Println("ERROR CLOSING THE MSSQL CONNECTION:", err)
	   }
}

func getData(condb *sql.DB, items *[]gocb.BulkOp) () {
	var ID string
	var JSONData string
	query := "Select DocumentID ID, jSonData JSONData From LocalDB.dbo.Users"
	rows, err := condb.Query(query)
	if err != nil {
	   log.Fatal(err)
	   err = nil
	   }
	for rows.Next() {
	 err := rows.Scan(&ID,&JSONData)
	 if err != nil {
		 fmt.Println("ERROR:",err)
	 }
	 *items = append(*items, &gocb.UpsertOp{Key: ID, Value: JSONData})	 
	}	
}

func InsertData(bucket *gocb.Bucket, item *[]gocb.BulkOp) (){
	err := bucket.Do(*item)
	if err != nil {
	   fmt.Println("ERROR PERFORMING BULK INSERT:", err)
	   }
}

#2

Hey @krishan.jangid,

Note that gocb.Connect doesn’t actually establish the real server connections due to the bucket-centric APIs that the server uses. It is likely that your cluster is not working correctly on localhost (this is what the error indicates). You may wish to verify that your local cluster is indeed running by going to http://localhost:8091/.

Cheers, Brett


#3

Hi @brett19, it is a single server cluster which is up and accessible via web UI. When I modified the code by reading the data from flat files instead of SQL Server, the error was gone.