我遇到了恐慌:运行时错误:在Golang中的无效内存地址或空指针解引用。

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

I have got panic: runtime error: invalid memory address or nil pointer dereference in Golang

问题

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

当我添加以下代码时:

statusCOD, err := c.route.FindStatusCODByOrigin(ctx, req.Origin)
			if err != nil {
				if err != sql.ErrNoRows {
					ddlogger.Log(ctx, span, "Find status cod destination and cod origin", err)
					errChan <- err
					return
				}
			}

			if statusCOD != nil {
				IsCODOrigin = statusCOD.IsCODOrigin
				IsCODDestination = statusCOD.IsCODDestination
			}

在这个函数中:

for i, v := range detailShipments {
		var dtPackage repo.PackageBaseModel

		go func(idx int, vShipment repo.ShipmentDetailBaseModel, dataShipmentNew repo.ShipmentCODCreateModel) {
			defer wg1.Done()

			randomID := commonString.RandomWithCustomCharList(c.config.ShipmentCODIDRandom, c.config.ShipmentIDCharlist)
			shipmentID := fmt.Sprintf("%s%s", prefix, randomID)
			dataShipmentNew.ShipmentBaseModel.ShipmentID = strings.ToUpper(shipmentID)
			dataShipmentNew.ShipmentDetailBaseModel = vShipment
			var commodityName string

			sCategory, err := c.shipmentCategoryRepo.FindOneShipmentCategoryByID(ctx, vShipment.ShipmentCategoryID.Int64)

			if err != err && err != sql.ErrNoRows {
				ddlogger.Log(ctx, span, "shipmentService-CreateShipmentCOD "+shipmentID, " Failed shipmentCategoryRepo.FindOneShipmentCategoryByID", err)
			} else {
				if sCategory != nil {
					commodityName = sCategory.CommodityName.String
				}
			}

			req := &repo.TariffRequestModelV2{
				Origin:      form.DetailSender.Origin,
				Destination: vShipment.Destination,
				Weight:      vShipment.GrossWeight / 1000,
				Commodity:   commodityName,
				GoodsValue:  int64(vShipment.GoodValues.Float64),
				IsInsurance: vShipment.IsInsurance.Bool,
				Product:     vShipment.ProductType,
				Width:       vShipment.DimensionWidth.Float64,
				Height:      vShipment.DimensionHeight.Float64,
				Length:      vShipment.DimensionLength.Float64,
				IsCOD:       true,
				CODValue:    vShipment.CODValue.Float64,
			}

			tariff, err := c.coreSystemRepo.CheckTariffV3(ctx, req)
			if err != nil {
				ddlogger.Log(ctx, span, "[shipmentService-CreateShipmentCOD [coreSystemRepo.CheckTariffV3]]", err)
				errChan <- err
				return
			}
			statusCOD, err := c.route.FindStatusCODByOrigin(ctx, req.Origin)
			if err != nil {
				if err != sql.ErrNoRows {
					ddlogger.Log(ctx, span, "Find status cod destination and cod origin", err)
					errChan <- err
					return
				}
			}

			if statusCOD != nil {
				IsCODOrigin = statusCOD.IsCODOrigin
				IsCODDestination = statusCOD.IsCODDestination
			}
             ................
}
	wg1.Wait()
	close(shipmentChan)
	close(errChan)
	close(amountChan)
英文:

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

when i add

statusCOD, err := c.route.FindStatusCODByOrigin(ctx, req.Origin)
			if err != nil {
				if err != sql.ErrNoRows {
					ddlogger.Log(ctx, span, &quot;Find status cod destination and cod origin&quot;, err)
					errChan &lt;- err
					return
				}
			}

			if statusCOD != nil {
				IsCODOrigin = statusCOD.IsCODOrigin
				IsCODDestination = statusCOD.IsCODDestination
			}

in this func

for i, v := range detailShipments {
		var dtPackage repo.PackageBaseModel

		go func(idx int, vShipment repo.ShipmentDetailBaseModel, dataShipmentNew repo.ShipmentCODCreateModel) {
			defer wg1.Done()

			randomID := commonString.RandomWithCustomCharList(c.config.ShipmentCODIDRandom, c.config.ShipmentIDCharlist)
			shipmentID := fmt.Sprintf(&quot;%s%s&quot;, prefix, randomID)
			dataShipmentNew.ShipmentBaseModel.ShipmentID = strings.ToUpper(shipmentID)
			dataShipmentNew.ShipmentDetailBaseModel = vShipment
			var commodityName string

			sCategory, err := c.shipmentCategoryRepo.FindOneShipmentCategoryByID(ctx, vShipment.ShipmentCategoryID.Int64)

			if err != err &amp;&amp; err != sql.ErrNoRows {
				ddlogger.Log(ctx, span, &quot;shipmentService-CreateShipmentCOD &quot;+shipmentID, &quot; Failed shipmentCategoryRepo.FindOneShipmentCategoryByID&quot;, err)
			} else {
				if sCategory != nil {
					commodityName = sCategory.CommodityName.String
				}
			}

			req := &amp;repo.TariffRequestModelV2{
				Origin:      form.DetailSender.Origin,
				Destination: vShipment.Destination,
				Weight:      vShipment.GrossWeight / 1000,
				Commodity:   commodityName,
				GoodsValue:  int64(vShipment.GoodValues.Float64),
				IsInsurance: vShipment.IsInsurance.Bool,
				Product:     vShipment.ProductType,
				Width:       vShipment.DimensionWidth.Float64,
				Height:      vShipment.DimensionHeight.Float64,
				Length:      vShipment.DimensionLength.Float64,
				IsCOD:       true,
				CODValue:    vShipment.CODValue.Float64,
			}

			tariff, err := c.coreSystemRepo.CheckTariffV3(ctx, req)
			if err != nil {
				ddlogger.Log(ctx, span, &quot;[shipmentService-CreateShipmentCOD [coreSystemRepo.CheckTariffV3]]&quot;, err)
				errChan &lt;- err
				return
			}
			statusCOD, err := c.route.FindStatusCODByOrigin(ctx, req.Origin)
			if err != nil {
				if err != sql.ErrNoRows {
					ddlogger.Log(ctx, span, &quot;Find status cod destination and cod origin&quot;, err)
					errChan &lt;- err
					return
				}
			}

			if statusCOD != nil {
				IsCODOrigin = statusCOD.IsCODOrigin
				IsCODDestination = statusCOD.IsCODDestination
			}
             ................
}
	wg1.Wait()
	close(shipmentChan)
	close(errChan)
	close(amountChan)

答案1

得分: 1

看起来你刚刚创建了指针,但它没有指向任何内存空间。你可以检查你的新代码中的所有变量来找到它,例如 ctx。

英文:

It looks like you just created the pointer, and it doesn't point to any memory spaces, you can check all the variables in your new code to find it. e.g. ctx

huangapple
  • 本文由 发表于 2021年8月10日 14:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/68722028.html
匿名

发表评论

匿名网友

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

确定