go - Golang PutItem DynamoDB: Runtime Error invalid memory address or nil pointer dereference -
new programming golang , aws. block of code in function, trying out creating new table , writing values using aws dynamodb. creation successful, program crashes when write happens. not sure why..i'd grateful if me out!
**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 createtable transaction gets committed. time.sleep(2 * time.second) log.println("sleep 2 before write") testa := "batch_1" //value written 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) }
it helpful if show stack trace of error. until error occurs when try access member of uninitialized variable. example if error comming @ following line
a := b.c
then need check if b initialized properly. value of b nil hence nil pointer dereference.
Comments
Post a Comment