恐慌:运行时错误:无效的内存地址或空指针解引用(一次又一次)

huangapple go评论80阅读模式
英文:

panic: runtime error: invalid memory address or nil pointer dereference (time and again)

问题

我对golang非常陌生,大部分时间都不知道自己在做什么。我尝试运行一个简单的find()查询,从我的数据库中获取所有文档,但似乎无法使其工作,我一直收到这个错误:

[signal SIGSEGV: segmentation violation code=0x1 addr=0x58 pc=0x9e2623]

goroutine 40 [running]:
go.mongodb.org/mongo-driver/mongo.(*Collection).Find(0x0, 0xf9f2a0, 0xc0000a4008, 0xc07dc0, 0xc00034e1b0, 0x0, 0x0, 0x0, 0x42eda1, 0xcf4758, ...)
	/home/rashad/go/pkg/mod/go.mongodb.org/mongo-driver@v1.5.3/mongo/collection.go:1106 +0x63
github.com/RashadArbab/goServer/Routes.GetAll(0xc000432000, 0x0, 0x0)
	/home/rashad/goServer/Routes/handler.go:22 +0xf1
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0x60b8d06b, 0x1da784f8, 0x9bd981defeb4)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc000432000, 0x1feb2696b, 0x14d7d20)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/ctx.go:656 +0x88
github.com/gofiber/fiber/v2/middleware/logger.New.func2(0xc000432000, 0xc000342208, 0x4)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/middleware/logger/logger.go:155 +0x13f
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0x0, 0xc0003221e0, 0x14f6240)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc000432000, 0x0, 0x0)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/ctx.go:656 +0x88
github.com/gofiber/fiber/v2.(*App).registerStatic.func3(0xc000432000, 0xc000342208, 0x4)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:380 +0x180
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0xc000432000, 0x1da63ae5, 0x1da63ae500000004)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*App).handler(0xc0003ea000, 0xc000424000)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:155 +0x118
github.com/valyala/fasthttp.(*Server).serveConn(0xc0003ec000, 0xfa5940, 0xc00032a008, 0x0, 0x0)
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/server.go:2219 +0x1497
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0003ee140, 0xc000322140)
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:223 +0xc0
github.com/valyala/fasthttp.(*workerPool).getCh.func1(0xc0003ee140, 0xc000322140, 0xba7ae0, 0xc000322140)
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:195 +0x35
created by github.com/valyala/fasthttp.(*workerPool).getCh
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:194 +0x101

我通过了检查点1,但在检查点2之前卡住了。一切似乎都正常,我可以连接到数据库并ping它以确保连接成功,但是当我尝试从中获取一些数据时,它会抛出一个panic。

以下是一些相关的代码片段:

(我正在尝试ping handler.go中的GetAll()函数)

handler.go

package Routes

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"github.com/RashadArbab/goServer/Database"
	"github.com/gofiber/fiber/v2"
	"go.mongodb.org/mongo-driver/bson"
)

var DB = Database.DB
var ctx = context.TODO()

func GetAll(c *fiber.Ctx) error {

	fmt.Println("检查点1")
	var users []Database.User

	cursor, err := DB.Find(ctx, bson.M{})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("检查点2")

	defer cursor.Close(context.TODO())
	fmt.Println("检查点3")

	for cursor.Next(context.TODO()) {

		var user Database.User
		err := cursor.Decode(&user)
		if err != nil {
			log.Fatal(err)
		}

		users = append(users, user)
	}

	fmt.Println("检查点4")

	documents, _ := json.Marshal(users)
	return c.JSON(documents)

}

func CreateProd(c *fiber.Ctx) error {
	return c.SendString("这是创建产品")
}

func GetSingle(c *fiber.Ctx) error {
	id := c.Params("id")
	return c.SendString("这是创建单数" + id)
}

main.go

package main

import (
	"github.com/RashadArbab/goServer/Database"
	"github.com/RashadArbab/goServer/Routes"

	"github.com/gofiber/fiber/v2"
)

func main() {
	Database.Init()

	app := fiber.New()

	app.Static("/", "./AA-Frontend/build")

	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("你好")
	})

	Routes.SetupRoutes(app)

	app.Listen(":5000")
}

database.go

package Database

import (
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"

	"context"
	"fmt"
	"log"
)

var DB *mongo.Collection
var ctx = context.TODO()

func Init() *mongo.Collection {
	clientOptions := options.Client().ApplyURI("mongodb://localhost:27017/")
	client, err := mongo.Connect(ctx, clientOptions)
	if err != nil {
		log.Fatal(err)
	}
	err = client.Ping(ctx, nil)
	if err != nil {
		log.Fatal(err)
	} else {
		fmt.Println()
	}

	DB = client.Database("GoServer").Collection("users")
	fmt.Print(DB.Name())

	return DB
}
英文:

I am very new to golang and for the most part have no idea what im doing so far. I tried to run a simple find() query to get all documents from my databse and I cant seem to get it to work i keep getting this error

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

goroutine 40 [running]:
go.mongodb.org/mongo-driver/mongo.(*Collection).Find(0x0, 0xf9f2a0, 0xc0000a4008, 0xc07dc0, 0xc00034e1b0, 0x0, 0x0, 0x0, 0x42eda1, 0xcf4758, ...)
	/home/rashad/go/pkg/mod/go.mongodb.org/mongo-driver@v1.5.3/mongo/collection.go:1106 +0x63
github.com/RashadArbab/goServer/Routes.GetAll(0xc000432000, 0x0, 0x0)
	/home/rashad/goServer/Routes/handler.go:22 +0xf1
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0x60b8d06b, 0x1da784f8, 0x9bd981defeb4)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc000432000, 0x1feb2696b, 0x14d7d20)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/ctx.go:656 +0x88
github.com/gofiber/fiber/v2/middleware/logger.New.func2(0xc000432000, 0xc000342208, 0x4)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/middleware/logger/logger.go:155 +0x13f
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0x0, 0xc0003221e0, 0x14f6240)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc000432000, 0x0, 0x0)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/ctx.go:656 +0x88
github.com/gofiber/fiber/v2.(*App).registerStatic.func3(0xc000432000, 0xc000342208, 0x4)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:380 +0x180
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0xc000432000, 0x1da63ae5, 0x1da63ae500000004)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*App).handler(0xc0003ea000, 0xc000424000)
	/home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:155 +0x118
github.com/valyala/fasthttp.(*Server).serveConn(0xc0003ec000, 0xfa5940, 0xc00032a008, 0x0, 0x0)
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/server.go:2219 +0x1497
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0003ee140, 0xc000322140)
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:223 +0xc0
github.com/valyala/fasthttp.(*workerPool).getCh.func1(0xc0003ee140, 0xc000322140, 0xba7ae0, 0xc000322140)
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:195 +0x35
created by github.com/valyala/fasthttp.(*workerPool).getCh
	/home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:194 +0x101

I get past checkpoint 1 and get stuck before checkpoint 2. everything seems to work, I can connect to the database and ping it to make sure the connection is successful, but then when i go to get some data from it it throws a panic.

here are some relevant pieces of code

(I am trying to ping the GetAll() function in handler.go)

handler.go

package Routes

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"github.com/RashadArbab/goServer/Database"
	"github.com/gofiber/fiber/v2"
	"go.mongodb.org/mongo-driver/bson"
)

var DB = Database.DB
var ctx = context.TODO()

func GetAll(c *fiber.Ctx) error {

	fmt.Println("Checkpoint 1")
	var users []Database.User

	

	cursor, err := DB.Find(ctx, bson.M{})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Checkpoint 2")

	// Close the cursor once finished
	/*A defer statement defers the execution of a function until the surrounding function returns.
	simply, run cur.Close() process but after cur.Next() finished.*/

	defer cursor.Close(context.TODO())
	fmt.Println("Checkpoint 3")

	for cursor.Next(context.TODO()) {

		// create a value into which the single document can be decoded
		var user Database.User
		// & character returns the memory address of the following variable.
		err := cursor.Decode(&user) // decode similar to deserialize process.
		if err != nil {
			log.Fatal(err)
		}

		// add item our array
		users = append(users, user)
	}

	fmt.Println("Checkpoint 4")

	documents, _ := json.Marshal(users)
	return c.JSON(documents)

}

func CreateProd(c *fiber.Ctx) error {
	return c.SendString("This is create product")
}

func GetSingle(c *fiber.Ctx) error {
	id := c.Params("id")
	return c.SendString("this is create singular" + id)
}

main.go

package main

import (
	"github.com/RashadArbab/goServer/Database"
	"github.com/RashadArbab/goServer/Routes"

	"github.com/gofiber/fiber/v2"
)

func main() {
	Database.Init()

	app := fiber.New()

	app.Static("/", "./AA-Frontend/build")

	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("hello there")
	})

	Routes.SetupRoutes(app)

	app.Listen(":5000")
}

database.go

package Database

import (
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"

	"context"
	"fmt"
	"log"
)

var DB *mongo.Collection
var ctx = context.TODO()

func Init() *mongo.Collection {
	clientOptions := options.Client().ApplyURI("mongodb://localhost:27017/")
	client, err := mongo.Connect(ctx, clientOptions)
	if err != nil {
		log.Fatal(err)
	}
	err = client.Ping(ctx, nil)
	if err != nil {
		log.Fatal(err)
	} else {
		fmt.Println()
	}

	DB = client.Database("GoServer").Collection("users")
	fmt.Print(DB.Name())

	return DB
}

答案1

得分: 3

DBnil,因为在database.DB初始化之前,var DB=database.DB已经运行了。

直接使用database.DB

英文:

DB is nil, because var DB=database.DB runs before database.DB is initialized.

Use database.DB directly.

huangapple
  • 本文由 发表于 2021年6月3日 21:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/67822084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定