PublishRetweet object reference not set

Dec 19, 2013 at 3:25 PM
Edited Dec 19, 2013 at 3:27 PM
basically here is whats happening:

I am creating a stream:

stream.StartStream(token, tweet => {
            if (tweet.Media.Count > 0)
            {
                Console.WriteLine(tweet.Media[0].MediaURL);
                AddTweet(ref tweet);
                tweet.PublishRetweet();
            }
            else
            {
                Console.WriteLine("no url");
            }
            return 1 == 1;
        });
}
But when it comes to the retweet i get an object reference not set
Coordinator
Dec 19, 2013 at 8:00 PM
Edited Dec 19, 2013 at 8:01 PM
Hi,

I think you have a fair point. Currently Tweetinvi follows the Twitter API which when a retweet is received has the Media field set to null.
Consequently you are trying to access the .Count field of an object which is not referenced.
I might reconsider how to manage null in later versions.

Concerning your issue, I believe this code should work:
            stream.StartStream(token, tweet => {
            if (tweet.Media != null && tweet.Media.Any())
            {
                Console.WriteLine(tweet.Media[0].MediaURL);
                AddTweet(ref tweet);
                tweet.PublishRetweet();
            }
            else
            {
                Console.WriteLine("no url");
            }
            return 1 == 1;
        });
Kind Regards,
Linvi
Dec 20, 2013 at 9:29 AM
Edited Dec 20, 2013 at 9:30 AM
Hi Linvi,

I see what you have suggested but didnt seem to work, however i did not work and threw an exception that i can not catch for some reason, however i have got it to work by doing this instead of the tweet.PublishRetweet.

IUser tweetinviApi = new User(aTweet2.Creator.ScreenName, token);
List<ITweet> tweets = tweetinviApi.GetUserTimeline();

ITweet t = null;
foreach (ITweet a in tweets)
{
if (a.Id == aTweet2.Id)
{ t = a; break; }
}

// Retweet is the tweet posted on the TokenUser timeline
ITweet retweet = t.PublishRetweet();
Console.WriteLine("Published retweet");
Coordinator
Dec 20, 2013 at 1:12 PM
Hi,

I see what you have suggested but didnt seem to work, however i did not work

It seems that I have not understand your problem in this case. I would be happy to have further look into your problem but you'll need to provide more details, notably the type of stream you are using and potentially the filters you are using.

i did not work and threw an exception that i can not catch for some reason

Be careful, if you wish to catch an exception, you will need to do that within the lambda expression ( => { // Somewhere here }); ) because it the lambda is not called within the main thread which already captures the exceptions.

Linvi
Dec 20, 2013 at 1:14 PM
AHA!!!! great thank you Linvi!!! Great API BTW!!! :))))))