以下是相关代码:
公共类User.cs:
public void FindByID(int id)
{
Parser parser = new Parser(id);
ID = parser.FindID();
Name = parser.FindName();
Rating = parser.FindRating();
Photo = parser.FindPhoto();
Reputation = parser.FindReputation();
Group = parser.FindGroup();
PostCount = parser.FindPosts();
PostPerDay = parser.FindPostsPerDay();
JoinDate = parser.FindJoinDate();
Views = parser.FindViews();
LastActive = parser.FindLastActive();
Title = parser.FindTitle();
Age = parser.FindAge();
Birthday = parser.FindBirthday();
Sex = parser.FindSex();
}
公共类Parser.cs:
public Parser(int userID)
{
doc = XDocument.Load(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}", userID));
}
/***********************************************************
* Methods that scrape information individually for fields *
* ********************************************************/
public string FindID()
{
return doc.XPathSelectElement("/ipb/profile/id").Value;
}
public string FindName()
{
return doc.XPathSelectElement("/ipb/profile/name").Value;
}
/etc etc etc
如果我尝试搜索不存在的用户,则会抛出异常,因为doc永远不会被初始化。我不确定我应该抓住这个bug。也许我做事的方式让我处于这个位置。
我想学习。 :P谢谢!
编辑:这是Stacktrace:
at DICHeads.Parser.FindID() in c:\users\sergio\documents\visual studio 2010\Projects\DICHeads\DICHeads\Parser.cs:line 36
at DICHeads.User.FindByID(Int32 id) in c:\users\sergio\documents\visual studio 2010\Projects\DICHeads\DICHeads\User.cs:line 33
at DICHeads.MainWindow.button1_Click(Object sender, RoutedEventArgs e) in c:\users\sergio\documents\visual studio 2010\Projects\DICHeads\DICHeads\MainWindow.xaml.cs:line 36
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at DICHeads.App.Main() in c:\users\sergio\documents\visual studio 2010\Projects\DICHeads\DICHeads\obj\x86\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:1)
我会这样做:
答案 1 :(得分:1)
您的代码可以抛出NullReferenceException,因为如果找不到该元素,XPathSelectElement将返回null。
返回值
键入:System.Xml.Linq.XElement
一个XElement,或者为null。
将您的代码更改为:
public string FindID()
{
var element = doc.XPathSelectElement("/ipb/profile/id");
if (element == null) { return null; }
return element.Value;
}
同样适用于其他方法。
答案 2 :(得分:0)
有两种方法可以处理它。
您可以添加检查构造函数以查看是否已填充doc。如果没有创建新的默认实例,那么所有代码仍然可以工作,但不会返回任何结果。虽然一旦在构造函数之外,您可以使用if包装以下代码,以检查文档是否已加载。
您可以跳过默认实例,只需检查构造函数外部的文档,就像您在第一个解决方案中一样。