1

I'm trying to use a ListView within Main.xml and a ListViewRow (separate to this) however after running the application i receieve a >

System.NullReferenceException error message

I'm sure i ran this before and it worked.

I have tried several solutions from other posts and other sources however nothing is working.

I have made sure that the listview exists in the xml and that the ID is correct also.

Thanks for your help everyone

07-17 15:57:16.656 D/Mono ( 4823): AOT: image '/storage/emulated/0/Android/data/theProject.theProject/files/.override/inspector-temp/Xamarin.Interactive.Android.dll.so' not found: dlopen failed: library "/data/app/theProject.theProject-1/lib/x86_64/libaot-Xamarin.Interactive.Android.dll.so" not found 07-17 15:57:16.657 D/Mono ( 4823): AOT: image '/usr/local/lib/mono/aot-cache/amd64/Xamarin.Interactive.Android.dll.so' not found: dlopen failed: library "/data/app/theProject.theProject-1/lib/x86_64/libaot-Xamarin.Interactive.Android.dll.so" not found 07-17 15:57:16.657 D/Mono ( 4823): Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/theProject.theProject/files/.override/inspector-temp/Xamarin.Interactive.Android.dll'. 07-17 15:57:16.657 D/Mono ( 4823): Config attempting to parse: '/storage/emulated/0/Android/data/theProject.theProject/files/.override/inspector-temp/Xamarin.Interactive.Android.dll.config'. 07-17 15:57:16.657 D/Mono ( 4823): Config attempting to parse: '/usr/local/etc/mono/assemblies/Xamarin.Interactive.Android/Xamarin.Interactive.Android.config'. InspectorDebugSession(30): StateChange: EntryPointBreakpointHit -> BreakdanceInvoking Loaded assembly: /storage/emulated/0/Android/data/theProject.theProject/files/.override/inspector-temp/Xamarin.Interactive.dll [External] Loaded assembly: /storage/emulated/0/Android/data/theProject.theProject/files/.override/inspector-temp/Xamarin.Interactive.Android.dll [External] 07-17 15:57:16.683 D/Mono ( 4823): Assembly Ref addref Xamarin.Interactive.Android[0x7ff1cb07b100] -> mscorlib[0x7ff1e0ccf580]: 12 07-17 15:57:16.707 D/Mono ( 4823): Assembly Ref addref Xamarin.Interactive.Android[0x7ff1cb07b100] -> Xamarin.Interactive[0x7ff1cb0fdf80]: 2 07-17 15:57:16.719 D/Mono ( 4823): Assembly Ref addref Xamarin.Interactive.Android[0x7ff1cb07b100] -> Mono.Android[0x7ff1cb7c9200]: 2 07-17 15:57:16.945 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:16.945 D/Mono ( 4823): Searching for 'java_interop_jnienv_register_natives'. 07-17 15:57:16.945 D/Mono ( 4823): Probing 'java_interop_jnienv_register_natives'. 07-17 15:57:16.945 D/Mono ( 4823): Found as 'java_interop_jnienv_register_natives'. 07-17 15:57:16.958 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:16.958 D/Mono ( 4823): Searching for 'java_interop_jnienv_call_nonvirtual_void_method_a'. 07-17 15:57:16.958 D/Mono ( 4823): Probing 'java_interop_jnienv_call_nonvirtual_void_method_a'. 07-17 15:57:16.958 D/Mono ( 4823): Found as 'java_interop_jnienv_call_nonvirtual_void_method_a'. 07-17 15:57:16.963 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:16.964 D/Mono ( 4823): Searching for '_monodroid_gc_wait_for_bridge_processing'. 07-17 15:57:16.964 D/Mono ( 4823): Probing '_monodroid_gc_wait_for_bridge_processing'. 07-17 15:57:16.964 D/Mono ( 4823): Found as '_monodroid_gc_wait_for_bridge_processing'. 07-17 15:57:16.974 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:16.974 D/Mono ( 4823): Searching for 'java_interop_jnienv_is_same_object'. 07-17 15:57:16.974 D/Mono ( 4823): Probing 'java_interop_jnienv_is_same_object'. 07-17 15:57:16.974 D/Mono ( 4823): Found as 'java_interop_jnienv_is_same_object'. 07-17 15:57:16.980 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:16.980 D/Mono ( 4823): Searching for 'java_interop_jnienv_new_string'. 07-17 15:57:16.980 D/Mono ( 4823): Probing 'java_interop_jnienv_new_string'. 07-17 15:57:16.980 D/Mono ( 4823): Found as 'java_interop_jnienv_new_string'. 07-17 15:57:16.986 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:16.986 D/Mono ( 4823): Searching for 'java_interop_jnienv_get_static_field_id'. 07-17 15:57:16.986 D/Mono ( 4823): Probing 'java_interop_jnienv_get_static_field_id'. 07-17 15:57:16.986 D/Mono ( 4823): Found as 'java_interop_jnienv_get_static_field_id'. 07-17 15:57:16.990 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:16.990 D/Mono ( 4823): Searching for 'java_interop_jnienv_get_static_object_field'. 07-17 15:57:16.990 D/Mono ( 4823): Probing 'java_interop_jnienv_get_static_object_field'. 07-17 15:57:16.990 D/Mono ( 4823): Found as 'java_interop_jnienv_get_static_object_field'. Thread started: #3 InspectorDebugSession(30): HandleTargetEvent: ThreadStarted InspectorDebugSession(30): StateChange: BreakdanceInvoking -> BreakdanceStarted 07-17 15:57:17.121 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:17.121 D/Mono ( 4823): Searching for 'java_interop_jnienv_call_static_void_method_a'. 07-17 15:57:17.121 D/Mono ( 4823): Probing 'java_interop_jnienv_call_static_void_method_a'. 07-17 15:57:17.121 D/Mono ( 4823): Found as 'java_interop_jnienv_call_static_void_method_a'. 07-17 15:57:17.125 D/Mono ( 4823): Assembly Ref addref theProject[0x7ff1cb7c9100] -> Mono.Android[0x7ff1cb7c9200]: 3 07-17 15:57:17.200 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:17.200 D/Mono ( 4823): Searching for 'java_interop_jnienv_get_array_length'. 07-17 15:57:17.200 D/Mono ( 4823): Probing 'java_interop_jnienv_get_array_length'. 07-17 15:57:17.200 D/Mono ( 4823): Found as 'java_interop_jnienv_get_array_length'. Loaded assembly: MonoDroidConstructors [External] 07-17 15:57:17.305 D/Mono ( 4823): Assembly Ref addref theProject[0x7ff1cb7c9100] -> mscorlib[0x7ff1e0ccf580]: 13 07-17 15:57:17.412 D/Mono ( 4823): DllImport attempting to load: '__Internal'. 07-17 15:57:17.413 D/Mono ( 4823): DllImport loaded library '(null)'. 07-17 15:57:17.413 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:17.413 D/Mono ( 4823): Searching for 'monodroid_get_system_property'. 07-17 15:57:17.413 D/Mono ( 4823): Probing 'monodroid_get_system_property'. 07-17 15:57:17.413 D/Mono ( 4823): Found as 'monodroid_get_system_property'. 07-17 15:57:17.413 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:17.413 D/Mono ( 4823): Searching for 'monodroid_free'. 07-17 15:57:17.413 D/Mono ( 4823): Probing 'monodroid_free'. 07-17 15:57:17.413 D/Mono ( 4823): Found as 'monodroid_free'. 07-17 15:57:17.442 D/Mono ( 4823): DllImport searching in: '__Internal' ('(null)'). 07-17 15:57:17.442 D/Mono ( 4823): Searching for 'java_interop_jnienv_call_nonvirtual_object_method_a'. 07-17 15:57:17.442 D/Mono ( 4823): Probing 'java_interop_jnienv_call_nonvirtual_object_method_a'. 07-17 15:57:17.442 D/Mono ( 4823): Found as 'java_interop_jnienv_call_nonvirtual_object_method_a'. InspectorDebugSession(30): HandleTargetEvent: UnhandledException Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

MainActivity code:

enter code here

using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System;

namespace theProject
{
    [Activity(Label = "theProject", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private List<SomeItem> items = new List<SomeItem>();
        private ListView listView;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
                ListView listView = FindViewById<ListView>(Resource.Id.listView);


                items.Add(new SomeItem { Name = "item1"});
                items.Add(new SomeItem { Name = "anotherItem"});

                MyListViewAdapter myListViewAdapter = new MyListViewAdapter(this, items);
                listView.Adapter = myListViewAdapter;           


            }
    }
}


MyListViewAdapter class code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace theProject
{
    class MyListViewAdapter: BaseAdapter<SomeItem>
    {
        private Context context;
        private List<SomeItem> items;

        public MyListViewAdapter(Context theContext, List<SomeItem> theItems)
        {
            context = theContext;
            items = theItems;
        }

        public override SomeItem this[int position]
        {
            get
            {
                return items[position];
            }
        }

        public override int Count
        {
            get
            {
                return items.Count;
            }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View currentRow = convertView;

            if (currentRow == null)
            {
                currentRow = LayoutInflater.From(context).Inflate(Resource.Layout.ListviewRow, parent, false);
            }

            ImageView itemImageView = currentRow.FindViewById<ImageView>(Resource.Id.itemImage);
            itemImageView = items[position].Image;

            TextView itemNameTxtView = currentRow.FindViewById<TextView>(Resource.Id.itemName);
            itemNametxtView.Text = items[position].Name;



            return currentRow;
        }
    }
}
6a6179
  • 272
  • 4
  • 20
  • 3
    Code would help. That enormous stack trace does not. – Broots Waymb Jul 17 '17 at 16:07
  • That's a fair comment, thanks for the quick reply by the way – 6a6179 Jul 17 '17 at 16:08
  • Does the code from the custom list adapter class need to be included or could that be a cause for this error even though the ListView is in the MainActivity class and main.xml? – 6a6179 Jul 17 '17 at 16:22
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Owen Pauling Jul 17 '17 at 16:25
  • No I've checked that link Owen and I understand what a NullReferenceException is, it's just that it's difficult to debug this particular one and there doesn't appear to be any obvious or logical problems... thanks for the link anyway – 6a6179 Jul 17 '17 at 16:34
  • Have you stepped through your OnCreate method with breakpoints on each line and seen how far you get? I'm just wondering if we can narrow down where it's crashing at so we can figure out why it's crashing. – Will Decker Jul 17 '17 at 16:42
  • can you show code of MyListViewAdapter – Yuri S Jul 17 '17 at 18:59
  • I have stepped through the code deckertron as that's what i was trying to find out also however it appears to skip some of the code when i do that also and just displays the error message. – 6a6179 Jul 17 '17 at 21:09
  • sure, no problem Yuri S – 6a6179 Jul 17 '17 at 21:09
  • @6a6179 did you know that when debugging with Visual Studio it is possible to use ["step over" and "step into"](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)? – Chedy2149 Jul 17 '17 at 22:00
  • @Chedy2149 Yes i am fully aware of that and have been using that feature for quite a few years however i cannot find the solution to this hence for my post. Thanks for the comment anyway though... just to add when i debug, VS for some reason skips some of the code even though I'm stepping into and not stepping over, which has never been an issue for me when using VS before – 6a6179 Jul 17 '17 at 22:34
  • So for some reason whilst debugging, the ListView FindViewById... line of code is skipped when stepping into within the MainActivity class – 6a6179 Jul 17 '17 at 22:45
  • @6a6179 Keep stepping through the program. There is some indirection running the program, so surely you will come back to `FindViewById`. – user838494 Jul 18 '17 at 12:21
  • @user5661402 i have used the 'step into' function whilst debugging however that line of code is skipped or not highlighted and I'm not entirely sure why... also the listview variable is null – 6a6179 Jul 18 '17 at 20:56
  • bundle is also null (just as an update to the debugging) – 6a6179 Jul 18 '17 at 21:04

0 Answers0