jjzjj

Golang PutItem DynamoDB : Runtime Error invalid memory address or nil pointer dereference

coder 2024-07-13 原文

刚开始使用 golang 和 AWS 进行编程。 我函数中的代码块,尝试创建一个新表并编写 使用 AWS DynamoDB 为其赋值。 创建成功,但是写的时候程序崩溃了。 不知道为什么..如果有人能帮助我,我将不胜感激!

**Logs**:

2015/07/22 15:46:46 TableStatus: 0xc208193cb0
2015/07/22 15:46:46 End
2015/07/22 15:46:48 Sleep 2: Before Write
2015/07/22 15:46:48 Before Defining Input
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x401b28]

**Code Block:**
 {
    log.Println("Entry+++")
        cfg := aws.DefaultConfig

        svc := dynamodb.New(cfg)

        tableDefinition := &dynamodb.CreateTableInput{
            TableName:            aws.String("table1"),
            AttributeDefinitions: make([]*dynamodb.AttributeDefinition, 1, 1),
            KeySchema:            make([]*dynamodb.KeySchemaElement, 1, 1),
            ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
                ReadCapacityUnits:  aws.Long(1),
                WriteCapacityUnits: aws.Long(1),
            },
        }

        tableDefinition.KeySchema[0] = &dynamodb.KeySchemaElement{
            AttributeName: aws.String("batch_id"),
            KeyType:       aws.String("HASH"),
        }
        tableDefinition.AttributeDefinitions[0] = &dynamodb.AttributeDefinition{
            AttributeName: aws.String("batch_id"),
            AttributeType: aws.String("S"),
        }

        resp, err := svc.CreateTable(tableDefinition)
        log.Println("After CreateTable---")

        if err != nil {
            log.Println("create table failed", err.Error())
            return
        }
        if resp != nil && resp.TableDescription != nil {
            log.Println(
                "TableStatus:", resp.TableDescription.TableStatus)
        }

        log.Println("End")
        //Some time before the createTable transaction gets committed. 
        time.Sleep(2 * time.Second)
        log.Println("Sleep 2 Before Write")

        testA := "batch_1" //value to be written to the db
        // testB := "batch_name"
        // testC := "530"
        // testD := "Sample-Keyy-98"

        log.Println("Before Defining Input")
        input := &dynamodb.PutItemInput{
            TableName: aws.String("table1"),
            Item: map[string]*dynamodb.AttributeValue{
                "batch_id": &dynamodb.AttributeValue{
                    S: aws.String(testA),
                },
                // "name": &dynamodb.AttributeValue{
                //  S: aws.String(testB),
                // },
            },
        }

        _, err2 := svc.PutItem(input)
    }

最佳答案

如果您能显示错误的堆栈跟踪,那将非常有帮助。但在那之前我可以说这个错误通常发生在你试图访问任何未初始化变量的成员时。 例如,如果此错误出现在以下行

a := b.c

然后你需要检查 b 是否被正确初始化。 b 的值很可能是 nil,因此 nil 指针取消引用。

关于Golang PutItem DynamoDB : Runtime Error invalid memory address or nil pointer dereference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31568081/

有关Golang PutItem DynamoDB : Runtime Error invalid memory address or nil pointer dereference的更多相关文章

随机推荐