2011-11-07 16 views
0

現在、このページhttp://www.silverlightshow.net/items/Silvester-A-Silverlight-Twitter-Widget.aspxを使用して、ユーザーのTwitterフィードを取得して表示するSilverlightアプリケーションを作成しています。Silverlight WebプロジェクトのTwitter Webサービス

私はコードにいくつか変更を加えましたが、それでも同じように動作するはずです。

>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: 

私はなぜこれが起こっているうちに動作しないことができます。私は、XMLを変換しようとすると、しかし、私はこのエラーメッセージを受信して​​います。コードは次のとおりです。

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; 
      } 

デバッガによると、これは選択された新しいTwitterItemを開始する行です。

ご協力いただければ幸いです!

+0

Trim()を呼び出す前に、値がnullでないことを確認しますか? – ChrisWay

答えて

1

NullReferenceExceptionあなたは、あなたがTwitterItemsを作成するのはかなり高度なコードを持ってnull.Member

ような何かを行う別の方法にそれを抽出して、あなたの前にすべての可能なヌルオブジェクト(あなたのケースイン要素)にはnullチェックを実行しようとすると、通常発生します.Element または.Trim()

のようなメンバーメンバー関数も、.Descendantsを呼び出す前にxmlDataがnullでないことを確認してください。

+0

ありがとう!メッセージ、画像ソース、およびユーザ名をコメントアウトすると、エラーが発生しません。だから問題はそこにある.. –

+0

あなたの診断がスポットにあった!どうもありがとうございました! –

関連する問題