5

I've already checked every similar question on Stackoverflow. None of them either answer or really ask the same question.

Is there a way to find the method that attempts to reference said null object?

Exception popup

I understand it can't say what object since it doesn't know, however it must know what method is attempting the null reference, but won't show it. In debug mode the code screen says:

"Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)."

I hit Continue execution to read the output and find it from there, however there was nothing relevant in the output:

**System.NullReferenceException:** 'Loading...'

Thread finished: <Thread Pool> #4
The thread 0x4 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #5
The thread 0x5 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #2
The thread 0x2 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #3
The thread 0x3 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #6
The thread 0x6 has exited with code 0 (0x0).
Thread started: <Thread Pool> #7
Thread started: <Thread Pool> #8
Thread started: <Thread Pool> #9
11-06 14:17:23.932 D/Mono    ( 5445): DllImport attempting to load: '/system/lib/liblog.so'.
11-06 14:17:23.933 D/Mono    ( 5445): DllImport loaded library '/system/lib/liblog.so'.
11-06 14:17:23.933 D/Mono    ( 5445): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
11-06 14:17:23.933 D/Mono    ( 5445): Searching for '__android_log_print'.
11-06 14:17:23.933 D/Mono    ( 5445): Probing '__android_log_print'.
11-06 14:17:23.933 D/Mono    ( 5445): Found as '__android_log_print'.
11-06 14:17:23.936 I/MonoDroid( 5445): UNHANDLED EXCEPTION:
11-06 14:17:23.938 I/MonoDroid( 5445): System.NullReferenceException: Object reference not set to an instance of an object.
11-06 14:17:23.938 I/MonoDroid( 5445):   at Xamarin.Forms.Grid.NumberOfUnsetRowHeight (Xamarin.Forms.BindableObject child) [0x00014] in D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:627 
11-06 14:17:23.938 I/MonoDroid( 5445):   at Xamarin.Forms.Grid.MeasureStarredRows () [0x0007b] in D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:545 
11-06 14:17:23.938 I/MonoDroid( 5445):   at Xamarin.Forms.Grid.MeasureAndContractStarredRows (System.Double width, System.Double height, System.Double totalStarsHeight) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:405 
11-06 14:17:23.938 I/MonoDroid( 5445):   at Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x00117] in D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:512 
11-06 14:17:23.938 I/MonoDroid( 5445):   at Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] in D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:60 
11-06 14:17:23.938 I/MonoDroid( 5445):   at Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:762 
11-06 14:17:23.938 I/MonoDroid( 5445):   at Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:644 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:132 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:702 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.StackLayout+LayoutInformation layout, Xamarin.Forms.StackOrientation orientation, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint) [0x000a8] in D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:163 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.StackLayout+LayoutInformation layout, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint, System.Boolean processExpanders) [0x00058] in D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:123 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.StackLayout.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0004e] in D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:58 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x00158] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:266 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:224 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:784 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Layout.ForceLayout () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Layout.cs:125 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Platform.Android.ViewCellRenderer+ViewCellContainer.Update (Xamarin.Forms.ViewCell cell) [0x00185] in D:\a\1\s\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:178 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Platform.Android.ViewCellRenderer.GetCellCore (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00027] in D:\a\1\s\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:21 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Platform.Android.CellRenderer.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00075] in D:\a\1\s\Xamarin.Forms.Platform.Android\Cells\CellRenderer.cs:51 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Platform.Android.CellFactory.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context, Xamarin.Forms.View view) [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Cells\CellFactory.cs:20 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Xamarin.Forms.Platform.Android.ListViewAdapter.GetView (System.Int32 position, Android.Views.View convertView, Android.Views.ViewGroup parent) [0x00200] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:299 
11-06 14:17:23.939 I/MonoDroid( 5445):   at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 position, System.IntPtr native_convertView, System.IntPtr native_parent) [0x0001a] in <11a340ccc8de43f09c97400139266ef5>:0 
11-06 14:17:23.939 I/MonoDroid( 5445):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.68(intptr,intptr,int,intptr,intptr)
11-06 14:17:23.950 W/.TheStylingRoo( 5445): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
11-06 14:17:23.954 D/Mono    ( 5445): DllImport searching in: '__Internal' ('(null)').
11-06 14:17:23.955 D/Mono    ( 5445): Searching for 'java_interop_jnienv_throw'.
11-06 14:17:23.955 D/Mono    ( 5445): Probing 'java_interop_jnienv_throw'.
11-06 14:17:23.955 D/Mono    ( 5445): Found as 'java_interop_jnienv_throw'.
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

This one has me stumped. The exception triggers when trying to load a page that displays a list of clients and another that loads bookings. Note that the startup page has the same logic to load list of current bookings, and works fine.

I've scoured every related question and tried the following solutions:

  • Stepping through code. It triggers at the end of either of 2 page constructors, not including the startup page constructor. All methods within those constructors execute fine.
  • Looking for nulls. No objects or properties are null.
  • Commenting, try catching and if !'s. None of these three prevent the exception.
  • Recreating xaml events. I was sure it was this, but nope.
  • Looking at the exception details. The Exception popup has no details at all.
  • Changing the async methods. The startup view that displays bookings is virtually a carbon copy, including async methods. It works fine.
  • Removing Bindings.
  • Clean, rebuild, Factory reset of Android emulator. In various orders
  • Restarting Visual Studio
  • Deleting bin folder and reloading

Relevant code:

MainPage.xaml.cs (Just the event that loads the ClientsPage)

private async void Button_Clicked(object sender, EventArgs e)
        {
            try
            {
                await Navigation.PushAsync(new Clients_Page());
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

Clients_Page.xaml.cs (just the constructor)

public partial class Clients_Page : ContentPage
    {
        public Clients_Page()
        {
            InitializeComponent();
            Clients_ViewModel viewModel = new Clients_ViewModel();
            viewModel.LoadClients(); 
            BindingContext = viewModel;
// Commenting all but InitializeComponent (Including Bindings in xaml) has no effect, so the ViewModel is not to blame
        } // EXCEPTION FIRES AFTER THIS LINE

Clients_ViewModel.cs

class Clients_ViewModel : INotifyPropertyChanged
    {
        //PROPERTIES_________________________________________________________
        public event PropertyChangedEventHandler PropertyChanged;
        private IRepository database { get; }
        private ObservableCollection<Client> clients;
        public ObservableCollection<Client> Clients
        {
            get { return clients; }
            set
            {
                if (value != clients)
                {
                    clients = value;
                    if (PropertyChanged != null)
                    {
                        NotifyPropertyChanged("Clients");
                    }
                }
            }
        }

        public async void LoadClients()
        {
                Clients = new ObservableCollection<Client>(await database.GetAllClients());

        }

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public Clients_ViewModel()
        {
            database = App.Database_IRepository;
        }

    }//endclass
}//endnamespace

Repository.cs (Just the GetAllClients Task)

        public async Task<List<Client>> GetAllClients()
        {
            return await database.Table<Client>().OrderBy(c => c.FirstName).ToListAsync();
        }

Xaml is just a ListView using ItemsSource="{Binding Clients}" with a ViewCell using Text="{Binding FirstName}"

I'm proficient with debugging, and would have fun locating the method with the null object reference. I just need to know how to get the debugger to reveal said method.

UPDATE 1: The exception fires even when all bindings in xaml are removed and the call to the viewmodel is removed. Also I've tried commenting the entire Clients_Page.xaml besides the ContentPage and main grid and the error still fires. So it cannot be the ViewModel or the xaml

UPDATE 2: Error occurs even when changing the click event to navigate to a fresh new page

8
  • could you get the Clients correctly?if yes,try to move Clients_ViewModel viewModel = new Clients_ViewModel(); viewModel.LoadClients(); BindingContext = viewModel; to onApearing method
    – Leo Zhu
    Commented Nov 6, 2019 at 5:12
  • if FirstName is the property of Client,you should use Text="{Binding FirstName}" in ViewCell
    – Leo Zhu
    Commented Nov 6, 2019 at 5:52
  • Clients has the 3 objects passed to it by the LoadClients method. Using OnAppearing had no effect. I already tried correcting the Clients.FirstName to FirstName, sorry I forgot to edit that. Thanks for commenting though
    – Jarrad
    Commented Nov 6, 2019 at 5:54
  • "there was nothing relevant in the output" -- huh? the stack trace is right there, and tells you the method that throws the exception is Xamarin.Forms.Grid.NumberOfUnsetRowHeight (Xamarin.Forms.BindableObject child) [0x00014] in D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:627. How is that not relevant? How is that not exactly the thing you are asking how to find? Commented Nov 6, 2019 at 6:05
  • I assumed it couldn't be that since I removed the HasUnevenRows and there are no grids inside the ListView. Also I removed all bindings in case it was trying to load the data before the ListView, but same error. I Googled it and no one has posted a question with that stacktrace line besides myself. Pretty much unless it is answered here or I figure it out, there is no online solution.
    – Jarrad
    Commented Nov 6, 2019 at 6:47

1 Answer 1

1

SOLVED

I had to remove NavigationPage.SetHasNavigationBar(this, false); from the Main_Page.xaml.cs constructor (Full block shown below)

public Main_Page()
        {
            InitializeComponent();
            //NavigationPage.SetHasNavigationBar(this, false);
            Main_ViewModel viewModel = new Main_ViewModel();
            viewModel.LoadCurrentBookings();
            BindingContext = viewModel;
        }

I added that line to remove the ugly Android navigation bar from showing when my app was running, however it appears that Android page navigation is somehow linked to that bar, so My startup view could not navigate to any other view with it set to false

1
  • Glad hear that *.^. By the way , if RootPage is not NavigationPage, you also can use Navigation.PushModalAsync(xxx); ,then it also will work . Finally , remember to mark answer later when it can . Commented Nov 6, 2019 at 7:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.