2010-07-04 103 views
0

下面是相關代碼:我在哪裏可以找到這個簡單的錯誤?

公共類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() 
+0

你可以粘貼異常,甚至可能是堆棧跟蹤?就像一個小例子,什麼有效,什麼不會幫助我們幫助你:) – npinti 2010-07-04 21:27:26

+0

例如,ID「1234」有效,但ID「22222222」不起作用。我將用實際的錯誤信息編輯我的問題。 – 2010-07-04 21:29:59

+0

很高興您發佈堆棧跟蹤,但您忘記了最重要的部分:異常類型和消息。它應該在您發佈的部分的正上方。我猜這是一個NullReferenceException,但很高興看到確認。 – 2010-07-04 21:38:30

回答

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

同樣地,對於您的其他方法。

0

有兩種方法可以處理它。

  1. 您可以添加一個檢查構造函數來查看doc是否已經填充。如果沒有創建一個新的默認實例,那麼你的所有代碼仍然可以工作,但不會返回任何結果。雖然一旦在構造函數之外,你可以用if來包裝下面的代碼,以檢查實際上是否加載了doc。

  2. 您可以跳過默認實例,只需像第一個解決方案中那樣檢查構造函數之外的文檔。

1

我會做它的方式:

  1. 遙,從構造一個沒有這樣的用戶異常(或類似)如果用戶不存在。
  2. 將構造函數調用包裝在try-catch塊中。
相關問題