nil pointer ref can't figure out why

75 views
Skip to first unread message

Alexander Mills

unread,
Jan 18, 2021, 3:12:05 PM1/18/21
to golang-nuts
I have this line:

fmt.Printlin("the dao:", dao);

and it logs:

the dao: &{<nil> <nil>}


the overall method looks like:

func (dao *UserAttributeDao) GetDecryptedUserAttributes(workflowID string) (*PayoutUserAttributeRecord, error) {
getItemInput := &dynamodb.GetItemInput{
TableName: aws.String(payoutUserAttributesTableName),
ConsistentRead: aws.Bool(true),
Key: map[string]*dynamodb.AttributeValue{
payoutUserAttributesPK: {S: aws.String(workflowID)},
},
}

fmt.Println("the dao:", dao) 

record, err := dao.GetItem(getItemInput)  // NIL POINTER REF HERE
if err != nil {
logrus.WithError(err).Errorf("error during GetItem from DynamoDB table %v for id: %v",
payoutUserAttributesTableName, workflowID)
return nil, err
}


does anyone know why calling the method would result in a nil pointer? To me it seems like the object for which the method is being called is nil, but that doesn't make that much sense to me. My main method looks like:

func main() {
log.Println("doing some printing")

var d = new(lib.UserAttributeDao)
x, err := d.GetDecryptedUserAttributes("");  // THIS RESULTS IN NIL POINTER

if err != nil {
log.Fatal(err)
}

}


....


Axel Wagner

unread,
Jan 18, 2021, 3:17:48 PM1/18/21
to Alexander Mills, golang-nuts
On Mon, Jan 18, 2021 at 9:12 PM 'Alexander Mills' via golang-nuts <golan...@googlegroups.com> wrote:
does anyone know why calling the method would result in a nil pointer? To me it seems like the object for which the method is being called is nil

No, it is a pointer to a struct with two fields, both of which are nil. It says `&{<nil> <nil>}`, not `<nil>`.
 
var d = new(lib.UserAttributeDao)

You are initializing `d` to a pointer, pointing at the zero value of `lib.UserAttributeDao` - which is a struct with two fields, I assume. So, for the zero value, both of those are nil.
 
x, err := d.GetDecryptedUserAttributes("");  // THIS RESULTS IN NIL POINTER

if err != nil {
log.Fatal(err)
}

}


....


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b1f9de70-fbe6-42ad-8cf5-7fb65d0908fcn%40googlegroups.com.

Axel Wagner

unread,
Jan 18, 2021, 3:18:36 PM1/18/21
to Alexander Mills, golang-nuts
You might want to use `Printf("the dao: %+v", dao)`, for example - it will also print type-names and should make it more obvious what you have.
Reply all
Reply to author
Forward
0 new messages