Search code examples
c#xamarin.androidandroid-edittextnullreferenceexception

EditText always set to null


I keep getting a "Object reference not set to an instance of an object".

Here is my code:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            payroll = JsonConvert.DeserializeObject<Payroll>(Intent.GetStringExtra("payroll"));
            EditText name2 = FindViewById<EditText>(Resource.Id.name2);
            EditText age2 = FindViewById<EditText>(Resource.Id.age2);
            EditText finalPCB2 = FindViewById<EditText>(Resource.Id.finalPCB2);
            EditText finalEPF2 = FindViewById<EditText>(Resource.Id.finalEPF2);
            EditText finalSOCSO2 = FindViewById<EditText>(Resource.Id.finalSOCSO2);
            EditText finalEIS2 = FindViewById<EditText>(Resource.Id.finalEIS2);
            EditText grossSalary2 = FindViewById<EditText>(Resource.Id.grossSalary2);
            EditText netSalary2 = FindViewById<EditText>(Resource.Id.netSalary2);
            EditText employerEPF2 = FindViewById<EditText>(Resource.Id.employerEPF2);
            EditText employerSOCSO2 = FindViewById<EditText>(Resource.Id.employerSOCSO2);
            EditText employerEIS2 = FindViewById<EditText>(Resource.Id.employerEIS2);
            name2.Text = " ";
            age2.Text = " ";
            finalPCB2.Text = " ";
            finalEPF2.Text = " ";
            finalSOCSO2.Text = " ";
            finalEIS2.Text = " ";
            grossSalary2.Text = " ";
            netSalary2.Text = " ";
            employerEPF2.Text = " ";
            employerSOCSO2.Text = " ";
            employerEIS2.Text = " ";

            Button _reviewBack = FindViewById<Button>(Resource.Id.reviewBack);

            _reviewBack.Click += PlayButton_Click;
            _reviewBack.Click += (sender, e) => {
                var payrollReview = new Intent(this, typeof(MainActivity));
                StartActivity(payrollReview);
            };

            void PlayButton_Click(object sender, EventArgs e)
            {
                MediaPlayer _player = MediaPlayer.Create(this, Resource.Drawable.buttonclick);
                _player.Start();
            }

        }

And here is my xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name2"
        android:textSize="50px"
        android:editable="false"
        android:background="@null"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/age2"
        android:textSize="50px"
        android:editable="false"
        android:background="@null"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:background="@null"
        android:id="@+id/finalPCB2"
        android:textSize="50px"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/finalEPF2"
        android:editable="false"
        android:background="@null"
        android:textSize="50px"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/finalSOCSO2"
        android:textSize="50px"
        android:editable="false"
        android:background="@null"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/finalEIS2"
        android:editable="false"
        android:background="@null"
        android:textSize="50px"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/grossSalary2"
        android:textSize="50px"
        android:editable="false"
        android:background="@null"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/netSalary2"
        android:textSize="50px"
        android:editable="false"
        android:background="@null"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Employer Details:"
        android:textSize="63px"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/employerEPF2"
        android:editable="false"
        android:background="@null"
        android:textSize="50px"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/employerSOCSO2"
        android:editable="false"
        android:background="@null"
        android:textSize="50px"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/employerEIS2"
        android:background="@null"
        android:textSize="50px"/>
    <TextView  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"
        android:layout_weight="1"/>  
    <Button
        android:id="@+id/reviewBack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Back To Employees"
        android:textSize="23dp"
        android:backgroundTint="@android:color/holo_green_dark"
        android:textColor="@android:color/white"/>
</LinearLayout>

When I hover over my name2 edittext it shows that it is null, like this:enter image description here

But it shouldn't be null, and I can't find the problem here.

Update: So it turns out I forgot to add the SetContentView in my OnCreate method, but I am still getting the same error. Am I putting the SetContentView in the right place?


Solution

  • Your code above seems to be missing SetContentView() in OnCreate() method and call FindViewById after it.

    It should look something like this:

    protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);            
            SetContentView(Resource.Layout.xxxx); //this is the layout XML for your activity
            EditText name2 = FindViewById<EditText>(Resource.Id.name2);
            ...
            name2.Text = ""; // here the name2 will not be null;
            
        }