Search code examples
gogolang-migrate

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


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, "Find status cod destination and cod origin", err)
                    errChan <- 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("%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)

Solution

  • 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