-1
using System;
using System.Collections.Generic;
using System.Drawing;
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 Gamemm1
{
    [Activity(Label = "ThirdActivity")]
    public class ThirdActivity : Activity
    {
        Button RannndomButton;
        TextView Information;
        TextView ResultInMillis;
        TextView ResultInMillisCount1;
        TextView EmptySpace;


        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.ThirdLayout);
            // Create your application here
            RannndomButton.FindViewById<Button>(Resource.Id.RannndomButton);
            Information.FindViewById<TextView>(Resource.Id.Information);
            ResultInMillis.FindViewById<TextView>(Resource.Id.ResultInMillis);
            ResultInMillisCount1.FindViewById<TextView>(Resource.Id.ResultInMillisCount1);
            EmptySpace.FindViewById<TextView>(Resource.Id.emptySpace);

            this.FindViewById<Button>(Resource.Id.RannndomButton).Click += RandomButtonIsClicked;



        }

        private void RandomButtonIsClicked(object sender, EventArgs e)
        {
            Information.Visibility = ViewStates.Invisible;
            ResultInMillis.Visibility = ViewStates.Invisible;
            ResultInMillisCount1.Visibility = ViewStates.Invisible;
            EmptySpace.Visibility = ViewStates.Invisible;

        }
    }
}

Hello guys, when I click on the button I get this exception: System.NullReferenceException: Object reference not set to an instance of an object. here: RannndomButton.FindViewById(Resource.Id.RannndomButton); . Waiting for your recommendations. Other 3buttons are working and I wrote them exactly same. I post that much of my code because I there is no way i did error in code so maybe problems is somewhere else. I tried to create additional AXML layout, so now id's doesnt even save. I can't access them at all by FindViewById. Whats the problem?

And my code in AXML is :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/Information"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:visibility="visible"
        android:text="Click on  the button to start, it will spawn at random location make sure to click it as fast as you can to win 5times." />
    <TextView
        android:id="@+id/emptySpace"
        android:layout_height="30dip"
        android:layout_width="30dip"
        android:text="          "
        android:layout_below="@id/Information"
        android:visibility="visible"
        android:layout_alignParentLeft="true" />
    <TextView
        android:id="@+id/ResultInMillis"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Reaction time in milliseconds: "
        android:layout_below="@id/emptySpace"
        android:visibility="visible"
        android:layout_alignParentLeft="true" />
    <TextView
        android:id="@+id/ResultInMillisCount1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text=" Will be displayed here"
        android:layout_below="@id/emptySpace"
        android:visibility="visible"
        android:layout_toRightOf="@id/ResultInMillis" />
    <Button
        android:id="@+id/RannndomButton"
        android:layout_height="75dip"
        android:layout_width="75dip"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#FFFFFF" />
</RelativeLayout>
Rectinho
  • 9
  • 1
  • 5
  • 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) – ADM Jun 11 '18 at 15:45
  • There are no duplicates – Rectinho Jun 11 '18 at 16:05

2 Answers2

0

Maybe changing objects that you are using for "findViewById"?

this.FindViewById<Button>(Resource.Id.RannndomButton);
this.FindViewById<TextView>(Resource.Id.Information);
this.FindViewById<TextView>(Resource.Id.ResultInMillis);
this.FindViewById<TextView>(Resource.Id.ResultInMillisCount1);
this.FindViewById<TextView>(Resource.Id.emptySpace);
Fran Dioniz
  • 1,134
  • 9
  • 12
0

You are not actually initializing your view objects. Change your OnCreate method as follows:

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.ThirdLayout);

    // Create your application here
    RannndomButton = this.FindViewById<Button>(Resource.Id.RannndomButton); 
    Information = this.FindViewById<TextView>(Resource.Id.Information); 
    ResultInMillis = this.FindViewById<TextView>(Resource.Id.ResultInMillis); 
    ResultInMillisCount1 = this.FindViewById<TextView>(Resource.Id.ResultInMillisCount1); 
    EmptySpace = this.FindViewById<TextView>(Resource.Id.emptySpace); 
    RannndomButton.Click += RandomButtonIsClicked; 
}
MilanG
  • 2,551
  • 16
  • 24