James Mundy
James Mundy

Reputation: 4329

Twitter web service in Silverlight Web Project

I am currently using this page http://www.silverlightshow.net/items/Silvester-A-Silverlight-Twitter-Widget.aspx to make a Silverlight application that gets a users twitter feed and displays it.

I have made some changes to the code but it should still work the same. However, I am receiving this error message when I try to convert the xml:

>System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=USElab.Web
  StackTrace:
       at USElab.Web.TwitterWebService.<GetUserTimeline>b__1(XElement status) in H:\USE\USE\USE\USElab\USElab.Web\TwitterWebService.asmx.cs:line 58
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at USElab.Web.TwitterWebService.GetUserTimeline(String twitterUser, String userName, String password) in H:\USE\USE\USE\USElab\USElab.Web\TwitterWebService.asmx.cs:line 57
  InnerException: 

I can't work out why this is happening. Here is the code:

   namespace USElab.Web
   {
/// <summary>
/// Summary description for TwitterWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]

public class TwitterWebService : System.Web.Services.WebService
{
    private const string UserTimelineUri = "http://twitter.com/statuses/user_timeline/{0}.xml";
    private const string StatusElementName = "status";
    private const string CreatedAtElementName = "created_at";
    private const string TextElementName = "text";
    private const string ProfileImageUrlElementName = "profile_image_url";
    private const string NameElementName = "name";
    private const string UserElementName = "user";
    private const string UserUrlElementName = "url";
    public const int RequestRateLimit = 70;

    [WebMethod]
        public List<TwitterItem> GetUserTimeline( string twitterUser, string userName, string password )
        {
              if ( string.IsNullOrEmpty( twitterUser ) )
              {
                  throw new ArgumentNullException( "twitterUser", "twitterUser parameter is mandatory" );
              }

              WebRequest rq = HttpWebRequest.Create( string.Format( UserTimelineUri, twitterUser ) );

              if ( !string.IsNullOrEmpty( userName ) && !string.IsNullOrEmpty( password ) )
              {
                  rq.Credentials = new NetworkCredential( userName, password );
              }

              HttpWebResponse res = rq.GetResponse() as HttpWebResponse;

              // rate limit exceeded
              if ( res.StatusCode == HttpStatusCode.BadRequest )
              {
                  throw new ApplicationException( "Rate limit exceeded" );
              }

              XDocument xmlData = XDocument.Load(XmlReader.Create(res.GetResponseStream()));
              List<TwitterItem> data = (from status in xmlData.Descendants(StatusElementName)
                           select new TwitterItem
                           {
                               Message = status.Element(StatusElementName).Element(TextElementName).Value.Trim(),
                               ImageSource = status.Element(StatusElementName).Element(ProfileImageUrlElementName).Value.Trim(),
                               UserName = status.Element(StatusElementName).Element(UserElementName).Value.Trim()
                           }).ToList();



                return data;
          }

According to the debugger, this is happening the line that starts select new TwitterItem.

Any help would be greatly appreciated!

Upvotes: 0

Views: 167

Answers (1)

Valentin Kuzub
Valentin Kuzub

Reputation: 12093

NullReferenceException usually occurs when you try to do something like null.Member

You got pretty advanced code of creating TwitterItems , extract it to another method and perform null checks on all possible null objects (elements ins your case)before you call member functions like .Element .Value or .Trim()

also check that xmlData isnt null before you call .Descendants on it.

Upvotes: 1

Related Questions