Object reference not set to an instance of an object

Nov 12, 2013 at 1:44 PM
Hi there,

I've been working on a C# program which uses Linq2Twitter to search tweets based on users' inputs. For a couple of days (after rearranging the code), I've been getting this exception :
"Object reference not set to an instance of an object"

Here is the code :
 public static void Search(string input) {
                var auth = new ApplicationOnlyAuthorizer {
                    Credentials = new InMemoryCredentials {
                        ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                        ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"],
                    }
                };

                auth.Authorize();

                var twitterCtx = new TwitterContext(auth);
               
               var searchResponse =
                (from search in twitterCtx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == input &&
                       search.Count == 5
                 select search)
                         .SingleOrDefault();

                var tweets =
                (from status in searchResponse.Statuses
                 select new Tweet {
                     User = status.User.Identifier.ScreenName,
                     Text = status.Text,
                     Avatar = status.User.ProfileImageUrl,
                     From = status.Place.FullName,
                 })
                   .ToList();
The part throwing the exception is the "var searchResponse" one.

Thank you for reading.
Coordinator
Nov 12, 2013 at 3:06 PM
Hi,

I don't see anything wrong with your query. Do you have a stack trace (e.g. ex.ToString())?

There's also a known issue with the previous release, so make sure you have the latest release.

If it turns out to be a 401 exception, you can get help from the LINQ to Twitter FAQ.

Follow @JoeMayo
Nov 13, 2013 at 7:20 AM
Edited Nov 13, 2013 at 7:21 AM
Thanks for your answer. I have version 2.1.10 downloaded from NuGet.
Here is the stack trace I get :
   at LinqToTwitter.TwitterQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
   at Prototype.Tools.Utilities.TweetSharpUtilities.Search(String input) in c:\Users\lilyh\Documents\Visual Studio 2012\Projects\Prototype\Prototype\Tools\Utilities.cs:line 87
   at Prototype.ViewModels.TweetSearchViewModel.Search() in c:\Users\lilyh\Documents\Visual Studio 2012\Projects\Prototype\Prototype\ViewModels\TweetSearchViewModel.cs:line 55
   at Prototype.ViewModels.TweetSearchViewModel.<.ctor>b__0(Object param) in c:\Users\lilyh\Documents\Visual Studio 2012\Projects\Prototype\Prototype\ViewModels\TweetSearchViewModel.cs:line 51
   at Prototype.Tools.RelayCommand.Execute(Object parameter) in c:\Users\lilyh\Documents\Visual Studio 2012\Projects\Prototype\Prototype\Tools\RelayCommand.cs:line 47
   at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
   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.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.RaiseTrustedEvent(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, WindowMessage 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, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   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 Prototype.App.Main() in c:\Users\lilyh\Documents\Visual Studio 2012\Projects\Prototype\Prototype\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
Coordinator
Nov 13, 2013 at 7:26 AM
Do you have the error message and exception type too?
Nov 13, 2013 at 7:49 AM
Well, the exact message is "Object reference not set to an instance of an object" and it's a "System.NullReferenceException".
Is that what you asked ?
Coordinator
Nov 13, 2013 at 8:10 AM
input should not be null.
Coordinator
Nov 13, 2013 at 8:18 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.