User.GetUserFromId

Mar 16, 2014 at 1:07 PM
Hi Linvi,

I use Tweetinvi in asp.net mvc project. When you first start I call User.GetUserFromId and all good, but after refresh page (F5) and call this function again I get exexption NullReferenceException. What could be the problem?

try
        {
            TwitterCredentials.SetCredentials(twitterAccessToken, 
                                                            twitterAccessTokenSecret, 
                                                            twitterAPIKey,
                                                            twitterAPISecret);

            var user = User.GetUserFromId(long.Parse(providerUserID));

            if (user != null)
            {
                return user.ProfileImageUrl;
            }
        }
        catch (Exception ex)
        {
             <exception here>
        }
Thank you.
Mar 16, 2014 at 2:48 PM
Hi Nata Raja,

Which variable does it say returns a null value?
Coordinator
Mar 16, 2014 at 6:27 PM
Hi,

Phoenix is right, we need to know what is returning null. We will also need to know what the last exception was.
ExceptionHandler.GetLastException();
Linvi
Mar 16, 2014 at 7:07 PM
I don't know. I see only Exception "An exception of type 'System.NullReferenceException' occurred in Tweetinvi.dll but was not handled in user code".
Mar 16, 2014 at 7:08 PM
Edited Mar 16, 2014 at 7:10 PM
Being able to see more code would help us to see where the problem might be occurring.

Is this a web app? If you refresh the page which I assume has a field to enter a user name, it won't have any value and then it will try to run.
        var user = User.GetUserFromId(null);
Mar 16, 2014 at 7:17 PM
linvi wrote:
Hi,

Phoenix is right, we need to know what is returning null. We will also need to know what the last exception was.
ExceptionHandler.GetLastException();
Linvi
I understand that it is necessary to know that returns null, but i don't know how. Last exception (ExceptionHandler.GetLastException()) return null :(
Mar 16, 2014 at 7:24 PM
Pheonix2105 wrote:
Being able to see more code would help us to see where the problem might be occurring.

Is this a web app? If you refresh the page which I assume has a field to enter a user name, it won't have any value and then it will try to run.
        var user = User.GetUserFromId(null);
Yes, this web app. I have in database twitterUserID, and call function with this parameter, so User.GetUserFromId(twitterUserID). First run web app do this good (cold run), after refresh page i got this exception.
Mar 16, 2014 at 7:26 PM
Full My code.

public class TwitterService
{
    public TwitterService()
    {
        TwitterCredentials.SetCredentials(
           ConfigurationManager.AppSettings["TwitterAccessToken"],
           ConfigurationManager.AppSettings["TwitterAccessTokenSecret"],
           ConfigurationManager.AppSettings["TwitterAPIKey"],
           ConfigurationManager.AppSettings["TwitterAPISecret"]);
    }

    public string GetImageUrl(string providerUserID)
    {
        try
        {
            var user = User.GetUserFromId(long.Parse(providerUserID));

            if (user != null)
            {
                return user.ProfileImageUrl;
            }
        }
        catch (Exception ex)
        {
        }

        return null;
    }
}
Mar 16, 2014 at 7:32 PM
Edited Mar 16, 2014 at 7:36 PM
Ok well firstly if your method doesn't return a value it should be set as void eg
  public void GetImageUrl(string providerUserID)
{
    try
    {
        var user = User.GetUserFromId(long.Parse(providerUserID));

        if (user != null)
        {
            return user.ProfileImageUrl;
        }
    }
    catch (Exception ex)
    {
    }
}
}
  1. Is the code to return the providerUserID functioning correctly? (Does it return a valid ID everytime?)
  2. You could also try Convert.ToInt64() instead of long.Parse() - I believe they both return the same though so It may not matter.
  3. The only reason I can think of what with what I've seen of your code is when you refresh the page the variable providerUserID is being unset on refresh.
Edit: Can't get it to format the code correctly but you want to remove "return null;" and replace "public string GetImageUrl(string providerUserID)" with " public void GetImageUrl(string providerUserID)" because you are not returning a string.
Mar 16, 2014 at 7:42 PM
Edited Mar 16, 2014 at 7:57 PM
Pheonix2105 wrote:
  1. Is the code to return the providerUserID functioning correctly? (Does it return a valid ID everytime?)
  2. You could also try Convert.ToInt64() instead of long.Parse() - I believe they both return the same though so It may not matter.
  3. The only reason I can think of what with what I've seen of your code is when you refresh the page the variable providerUserID is being unset on refresh.
Yes, I checked the variable and it's ok. Converting is right. The variable(userID) always have a value.
var userID = Convert.ToInt64(providerUserID);
                
var user = User.GetUserFromId(userID);

if (user != null)
      {
           return user.ProfileImageUrl;
      }
Coordinator
Mar 16, 2014 at 8:14 PM
Hi there,

Sorry for the delay, I am currently working on the next version of Tweetinvi! :p

Can you please let me know which line of code is invalid:
TwitterCredentials.SetCredentials(twitterAccessToken, twitterAccessTokenSecret, twitterAPIKey, twitterAPISecret);
var user = User.GetUserFromId(long.Parse(providerUserID));
return user.ProfileImageUrl;
In the meantime, I will try the same thing on an MVC application. Which version of MVC are you using 3, 4, 5?
Linvi
Mar 16, 2014 at 8:33 PM
1) line code is invalid.
var user = User.GetUserFromId(long.Parse(providerUserID));
2) Asp.net MVC 5

Thank you.
Coordinator
Mar 16, 2014 at 9:05 PM
Edited Mar 16, 2014 at 9:05 PM
Hi,

Thank you. Please give me some time, I will get back to you.

Linvi
Coordinator
Mar 16, 2014 at 10:02 PM
I am able to reproduce the bug, I will get back to you when I find out what is going wrong.

Linvi
Coordinator
Mar 16, 2014 at 10:12 PM
Hi,

This is a critical bug, I will fix it and release Tweetinvi 0.9.0.4 as soon as it is done.
Will keep up to date.

Linvi
Coordinator
Mar 16, 2014 at 10:26 PM
Edited Mar 16, 2014 at 10:27 PM
As a temporary fix you can do the following:
User.UserFactory.GetUserFromId(1577389800);
The fix is coming tonight.
Linvi
Coordinator
Mar 16, 2014 at 10:44 PM
Edited Mar 16, 2014 at 11:08 PM
Tweetinvi 0.9.0.4 is now available on nuget, it is also available from the download page.

Thank you for reporting the issue.
Linvi
Marked as answer by linvi on 3/16/2014 at 3:44 PM
Mar 17, 2014 at 1:04 AM
Thanks for update Linvi, It actually solved a very strange problem I was having too, My apps just gone from doing the strangest things to working sweet!