Silverlight web Project中的Twitter web服务
本文关键字:web 服务 Twitter Project Silverlight 中的 | 更新日期: 2023-09-27 18:13:39
我目前正在使用这个页面http://www.silverlightshow.net/items/Silvester-A-Silverlight-Twitter-Widget.aspx来制作一个Silverlight应用程序,该应用程序可以获取用户的twitter feed并显示它。
我已经做了一些改变的代码,但它应该仍然工作相同。但是,当我尝试转换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:
我不明白为什么会发生这种事。下面是代码:
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;
}
根据调试器,这发生在开始select new TwitterItem的那一行。
任何帮助都将非常感激!
NullReferenceException
通常发生在你试图做null.Member
时
创建TwitterItems的高级代码,将其提取到另一个方法,并在调用.Element
.Value
或.Trim()
等成员函数之前对所有可能的空对象(在您的情况下的元素)执行空检查
在调用.Descendants
之前还要检查xmlData是否为空