我在哪里可以找到这个简单的错误?
问题描述:
下面是相关代码:我在哪里可以找到这个简单的错误?
公共类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从不初始化。我不确定我应该在哪里发现这个错误。也许我做事情的方式让我处于这个位置。
我想了解。 :P谢谢!
编辑:这里是堆栈跟踪:
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()
答
您的代码可能会抛出一个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;
}
同样地,对于您的其他方法。
答
有两种方法可以处理它。
您可以添加一个检查构造函数来查看doc是否已经填充。如果没有创建一个新的默认实例,那么你的所有代码仍然可以工作,但不会返回任何结果。虽然一旦在构造函数之外,你可以用if来包装下面的代码,以检查实际上是否加载了doc。
您可以跳过默认实例,只需像第一个解决方案中那样检查构造函数之外的文档。
答
我会做它的方式:
- 遥,从构造一个没有这样的用户异常(或类似)如果用户不存在。
- 将构造函数调用包装在try-catch块中。
你可以粘贴异常,甚至可能是堆栈跟踪?就像一个小例子,什么有效,什么不会帮助我们帮助你:) – npinti 2010-07-04 21:27:26
例如,ID“1234”有效,但ID“22222222”不起作用。我将用实际的错误信息编辑我的问题。 – 2010-07-04 21:29:59
很高兴您发布堆栈跟踪,但您忘记了最重要的部分:异常类型和消息。它应该在您发布的部分的正上方。我猜这是一个NullReferenceException,但很高兴看到确认。 – 2010-07-04 21:38:30