Go - Invalid memory address or nil pointer dereference

-1

I'm aware of the previous posts made on SO, however I think my use case is different and I don't see flaws personally in my code. I'm getting an error Uploading to twitter...

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff7aa4b34cd]

goroutine 1 [running]:
main.createTwitterPost(0xc0000104c0, 0x32, 0xc00000c630, 0x2d, 0xc00000e2a0, 0x19, 0xc000010480, 0x32, 0xc000045700, 0x1, ...)
        C:/Users/Mikk/mirror/main.go:426 +0x3ad
main.main()
        C:/Users/Mikk/mirror/main.go:465 +0x39f
exit status 2

Line 426 refers to

fmt.Println("Creating tweet (PostID: " + post.ID + ")...")
    isNSFW := post.NSFW || post.Spoiler
    tweet, resp, err := tclient.Statuses.Update(post.Title+" https://redd.it/"+post.ID, &twitter.StatusUpdateParams{
        MediaIds:          []int64{res.MediaID},
        PossiblySensitive: &isNSFW,
    })

And line 465 refers to:

createTwitterPost(config, post, image, imageName)

Little backstory of the project, I have a twitter bot made in golang that tweets the most popular images from reddit. Here's my code that is throwing the error:

func createTwitterPost(config Conf, post *reddit.Post, eimg []byte, file string) {
    fmt.Println("Uploading", file, "to twitter...")

    oconfig := oauth1.NewConfig(config.Twitter.Conk, config.Twitter.Cons)
    token := oauth1.NewToken(config.Twitter.Token, config.Twitter.ToknS)
    tclient := twitter.NewClient(oconfig.Client(oauth1.NoContext, token))

    res, resp, err := tclient.Media.Upload(eimg, "image/jpeg")
    if err != nil {
        fmt.Fprintln(os.Stderr, "Fatal: Unable to upload image to Twitter! Error:\n", err)
        os.Exit(1)
    }
    resp.Body.Close()

    fmt.Println("Creating tweet (PostID: " + post.ID + ")...")
    isNSFW := post.NSFW || post.Spoiler
    tweet, resp, err := tclient.Statuses.Update(post.Title+" https://redd.it/"+post.ID, &twitter.StatusUpdateParams{
        MediaIds:          []int64{res.MediaID},
        PossiblySensitive: &isNSFW,
    })
    if err != nil {
        fmt.Fprintln(os.Stderr, "Fatal: Unable to create Tweet! Error:\n", err)
        os.Exit(1)
    }
    resp.Body.Close()

    fmt.Println("Tweet:\n\t"+tweet.Text, "\n\thttps://twitter.com/"+tweet.User.ScreenName+"/status/"+tweet.IDStr)
}

The error gets thrown after it prints "Uploading to Twitter" any ideas?

Cheers, Mikk

go
asked on Stack Overflow Apr 24, 2021 by Mikk Küttim • edited Apr 24, 2021 by Mikk Küttim

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0