Stack Exchange
Setting Text or Image gives NullReferenceException
As mentioned in the title I get a NullReferenceException when assigning value to an ImageView or TextView.
Here is the error I get:
07-20 11:33:04.833 I/MonoDroid(11396): UNHANDLED EXCEPTION:
07-20 11:33:04.843 I/MonoDroid(11396): System.NullReferenceException: Object reference not set to an instance of an object
07-20 11:33:04.843 I/MonoDroid(11396): at JittersApp.Droid.LayoutCodes.Drinks.listCompare (string,JittersApp.Droid.MainActivity) [0x00043] in d:\Docs\Eamonns App Development\App\App Core\JittersApp\JittersApp\JittersApp.Droid\LayoutCodes\Drinks.cs:61
07-20 11:33:04.843 I/MonoDroid(11396): at JittersApp.Droid.LayoutCodes.Drinks/<>c__DisplayClass2.<coreDrinks>b__1 (object,Android.Widget.AdapterView/ItemClickEventArgs) [0x00018] in d:\Docs\Eamonns App Development\App\App Core\JittersApp\JittersApp\JittersApp.Droid\LayoutCodes\Drinks.cs:45
07-20 11:33:04.843 I/MonoDroid(11396): at Android.Widget.AdapterView/IOnItemClickListenerImplementor.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) [0x0000d] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.AdapterView.cs:283
07-20 11:33:04.843 I/MonoDroid(11396): at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00019] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.AdapterView.cs:211
07-20 11:33:04.843 I/MonoDroid(11396): at (wrapper dynamic-method) object.f7f74bf0-78c1-4c81-a8d9-11a0a0130a01 (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x0009b>
07-20 11:33:04.853 W/dalvikvm(11396): VFY: unable to resolve direct method 44: Ljava/lang/Throwable;.<init> (Ljava/lang/String;Ljava/lang/Throwable;ZZ)V
07-20 11:33:04.863 W/dalvikvm(11396): threadid=1: thread exiting with uncaught exception (group=0x41a64438)
An unhandled exception occured.

My code looks as follows:
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;
using Android.Graphics;
using Android.Graphics.Drawables;

namespace JittersApp.Droid.LayoutCodes
{
    public class Drinks
    {
        void listSetUp()
        {

        }
        string[] items = new string[] { "Cat", "Mouse", "Dog" };

        public void coreDrinks(MainActivity mainAc)
        {

            //Home Click

            ImageView iv = mainAc.FindViewById<ImageView>(Resource.Id.homeDrinks);
            iv.Click += delegate
            {
                Resuables r = new Resuables();
                r.onHomeClick(mainAc);
            };

            IListAdapter la = new ArrayAdapter(mainAc, Android.Resource.Layout.SimpleListItem1, items);

            ListView lv = mainAc.FindViewById<ListView>(Resource.Id.drinksListView);
            lv.SetAdapter(la);

            lv.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>
            {
                String selectedFromList = lv.GetItemAtPosition(e.Position).ToString();
                listCompare(selectedFromList, mainAc);

            };

        }

        private void listCompare(string selectedFromList, MainActivity av)
        {
            Resuables r = new Resuables();
            if (selectedFromList == items[0])
            {
                ImageView iv = av.FindViewById<ImageView>(Resource.Id.itemPicture);
                //iv.SetImageResource(Resource.Drawable.JL);
                TextView tv = av.FindViewById<TextView>(Resource.Id.itemDes);
                //tv.SetText(Resource.String.firstItem);
                TextView price = av.FindViewById<TextView>(Resource.Id.textPrice);
                //price.SetText(Resource.String.three_pounds);
                av.SetContentView(Resource.Layout.ItemViewer);

            }
        }



    }
}

Lines 57, 59 and 61 (the commented lines) are causing the same error. Any ideas what can cause this?
It's annoying me because its the exact same as the code on the website (bar the fact its out of the MainActivity class, but that shouldn't matter if I have it set up the way I do?) Oh and the coreDrinks function gets called from the MainActivity class itself.
EDIT:
XML layout: 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="#ffa95d5d">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <ImageView
            android:src="@drawable/jb"
            android:layout_width="261.2dp"
            android:layout_height="103.4dp"
            android:id="@+id/imageView1"
            android:layout_marginRight="0.0dp"
            android:scaleType="fitXY" />
        <ImageView
            android:src="@drawable/basket_shopping_2"
            android:layout_width="48.5dp"
            android:layout_height="match_parent"
            android:id="@+id/imageView2" />
    </LinearLayout>
</LinearLayout>

MainActivity:
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using JittersApp.Droid.LayoutCodes;


namespace JittersApp.Droid
{
    [Activity(Label = "JittersApp.Droid", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]
        public class MainActivity : Activity
        {

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

                        // Set our view from the "main" layout resource
                        SetContentView (Resource.Layout.Main);
            Core();
                }

        public void Core()
        {
            //Drinks Button
            Button dk = FindViewById<Button>(Resource.Id.drinksBtn);
            dk.Click += delegate
            {
                SetContentView(Resource.Layout.Drinks);
                Drinks dr = new Drinks();
                dr.coreDrinks(this);
            };

        }

        }
}

EDIT: After so working I discovered the error is cause by the setContentView being at the bottom of the list(so not yet enabled) and in doing this everything on it is a null until it is. So I moved it to the top of function.

Can you also provide activity code and xml?

Yep - xml : http://pastebin.com/jjJUUzHw and activity code : http://pastebin.com/jF5YySC5

Please don't use pastebin. I have edited the question not to use it.

Thanks Cheesebaron, I'm still new here and the pasting code thing is a bit weird for me :)

After so working I discovered the error is cause by the setContentView being at the bottom of the list(so not yet enabled) and in doing this everything on it is a null until it is. So I moved it to the top of function.

The problem is simple - in your xml there no user controls you are looking for in your code. For example line 51: "itemPicture", but there is no such control her http://pastebin.com/jjJUUzHw
I see that you have two layouts - Resource.Layout.Main and Resource.Layout.Drinks but you've submitted only one xml, so may be I'm barking on the wrong tree.

I don't know how to look in the code from xml so I've been trying to do it all through the main activity and extending it out where needed. If you want I can share the xml code for main and drinks but main is just a button linked to drinks which is a list view which is what's trying to set the values on the itemviewer then display it

Can you share the full project? On GitHub for example?

You've got all the code bar one class which isn't important as all it does is sent a contentview when an image is click and the two xml i mentioned above

Ok, so your layout is missing controls you are looking for.

I don't know, the images and textviews are there and when i don't set them in code(which makes the errors disappear but is not dynamic) the layout runs fine
