Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I was making a sample android app to test things out and the error is as follows on creating a button handler, though my build succeeded.

Please help

Error: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. occurred

Code is as follows:

using Android.App;
using Android.Widget;
using Android.OS;

namespace Android_Picture
{
[Activity(Label = "Android Picture", MainLauncher = true, Icon = 
"@drawable/icon")]
public class MainActivity : Activity
{
    Button ButtonPrev;
    Button ButtonNext;
    TextView TextTitle;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

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

        ButtonPrev = FindViewById<Button>(Resource.Id.buttonPrev);
        ButtonNext = FindViewById<Button>(Resource.Id.buttonNext);
        TextTitle = FindViewById<TextView>(Resource.Id.textTitle);

        ButtonPrev.Click += ButtonPrev_Click; //error
        ButtonNext.Click += ButtonNext_Click;
    }

    private void ButtonNext_Click(object sender, System.EventArgs e)
    {
        TextTitle.Text = "Next Clicked";
        //throw new System.NotImplementedException();
    }

    private void ButtonPrev_Click(object sender, System.EventArgs e)
    {
        TextTitle.Text = "Previous Clicked";
        //throw new System.NotImplementedException();
    }
}
}




My Main.axml is as follows:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
    android:text="Prev"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonPrev"
    android:layout_alignParentBottom="true" />
<Button
    android:text="Next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonNext"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true" />
<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textTitle"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="150dp" />
</RelativeLayout>


What I have tried:

I tried uncommenting the line

SetContentView (Resource.Layout.Main);


But still getting this error

Please help!!!
Posted
Updated 11-Apr-17 13:04pm
Comments
Richard Deeming 11-Apr-17 16:15pm    
The error message is pretty obvious: you're trying to modify a variable which is null.

That means the FindViewById<Button>(Resource.Id.buttonPrev) call returned null for some reason.

To avoid the error, test whether the button is null before trying to attach an event handler.

To resolve the problem, debug your code and find out why the button isn't being found.
Afzaal Ahmad Zeeshan 11-Apr-17 19:07pm    
Because he had intentionally hid the button, his layout inflation was commented out in the code that he has shared. Which is why, the button never got inflated and not found; see my answer as well.

1 solution

Although to handle the errors, you would need to use a try...catch block, whereas in this case it would be an if...else block to be used.
ButtonPrev = FindViewById<Button>(Resource.Id.buttonPrev);
ButtonNext = FindViewById<Button>(Resource.Id.buttonNext);
TextTitle = FindViewById<TextView>(Resource.Id.textTitle);

if(ButtonPrev != null) {
   ButtonPrev.Click += ButtonPrev_Click; // Would only execute, if not null.
}

// Same here.
// ButtonNext.Click += ButtonNext_Click;

This way, you will make sure that the event handlers are only associated if the objects exist, because if the buttons do not exist — there are several reasons for this, such as wrong ID being used, buttons not available in current layout, problem inflating the layout, ID different etc. — you do not need to even implement any handler on them. That would also make sure that the errors do not get raised.

The main error

Whereas, the main problem in your code is that you have commented out the layout; if the layout is included, and the button exists, there is no way that a button would end up null. Where as, in your code, you have intentionally done that.
// SetContentView (Resource.Layout.Main);

Uncomment this line, and it would work.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900