fasi fasi - 24 days ago 10
Android Question

Xamarin android unable to assign text value to edit text

I am new to mobile development. I am working on android in xamarin using visual studio 2015. For now i am following a sample code and doing the same as described in it.

I am getting an exception when i try to assign a

text
value to a
edit text
in my
update
layout. Bellow is the code in which i am assigning the values to
edit text


void List_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
click_Employee = e.Position + 1;
ICursor c = dbHelper.getSingleEntry(click_Employee);
c.MoveToFirst();
name = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_NAME));
email = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_EMAIL));
phone = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_PHONE));
designation = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_DESIGNATION));
dName.Text = name;
dEmail.Text = email;
dPhone.Text = phone;
dDesignation.Text = designation;
}


In above code the exception is raised at
dName.Text = name;
Bellow is the error description

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


Same work is performed in my
show complete data
function but there the things are good, don't know what's happening in
update
function

Update 1

Bellow is my
insert employee
code

namespace AppwithDB.Resources
{
[Activity(Label = "insertEmployee")]
class insertEmployee : Activity
{
EditText name, email, phone, designation;
Button submitBtn;
private SQLiteHelper dbHelper;

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

//Create your app here

SetContentView(Resource.Layout.insert);
initialize();
dbHelper = new SQLiteHelper(this);
submitBtn.Click += submitBtn_Click;
}


private void submitBtn_Click(object sender, EventArgs e)
{
if (name.Text.ToString().Equals("") || email.Text.ToString().Equals("")|| phone.Text.ToString().Equals("") || designation.Text.ToString().Equals(""))
{
Toast.MakeText(this, "Fields Empty Found", ToastLength.Short).Show();
}
else
{
dbHelper.insertEmployee(name.Text.ToString(), email.Text.ToString(), phone.Text.ToString(), designation.Text.ToString());
Toast.MakeText(this, "Data Stored Successfully", ToastLength.Short).Show();
name.Text = " ";
email.Text = " ";
phone.Text = " ";
designation.Text = " ";
}
}

private void initialize()
{
name = (EditText)FindViewById(Resource.Id.empName);
email = (EditText)FindViewById(Resource.Id.empEmail);
phone = (EditText)FindViewById(Resource.Id.empPhone);
designation = (EditText)FindViewById(Resource.Id.empDesignation);
submitBtn = (Button)FindViewById(Resource.Id.insertEmpBtn);
}
}}


Update 2

Bellow is the code for
update employee


namespace AppwithDB.Resources
{
[Activity(Label = "updateEmployee")]
class updateEmployee:Activity
{
ListView list;
EditText dName, dEmail, dPhone, dDesignation;
String name, email, phone, designation;
SQLiteHelper dbHelper;
int click_Employee;
Button upBtn;

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

// Create your application here
SetContentView(Resource.Layout.update);
initialize();
dbHelper = new SQLiteHelper(this);
System.Collections.ArrayList dataList = dbHelper.getALLEmployeesData();
string[] myArr = (string[])dataList.ToArray(typeof(string));
list.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleExpandableListItem1, myArr);

list.ItemClick += List_ItemClick;

upBtn.Click += delegate
{
dbHelper.updateEmployeeInfo(click_Employee, dName.Text.ToString(), dEmail.Text.ToString(), dPhone.Text.ToString(), dDesignation.Text.ToString());
};

}

private void initialize()
{
list = (ListView)FindViewById(Resource.Id.listView1);
dName = (EditText)FindViewById(Resource.Id.eName);
dEmail = (EditText)FindViewById(Resource.Id.eEmail);
dPhone = (EditText)FindViewById(Resource.Id.ePhone);
dDesignation = (EditText)FindViewById(Resource.Id.eDesignation);
upBtn = (Button)FindViewById(Resource.Id.updateEmploy);
}
void List_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
click_Employee = e.Position + 1;
ICursor c = dbHelper.getSingleEntry(click_Employee);
c.MoveToFirst();
name = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_NAME));
email = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_EMAIL));
phone = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_PHONE));
designation = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_DESIGNATION));
dName.Text = name;
dEmail.Text = email;
dPhone.Text = phone;
dDesignation.Text = designation;
}


}}


Update 3 :

Bellow are the
xmls
of
complete_data
and
update


<?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"
android:padding="10dp">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:divider="#000"
android:dividerHeight="1dp" />
<TextView
android:id="@+id/eName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="@+id/eEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
<TextView
android:id="@+id/ePhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<TextView
android:id="@+id/eDesignation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Designation" />
</LinearLayout></ScrollView></LinearLayout>


Update 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"
android:padding="10dp">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:divider="#000"
android:dividerHeight="1dp" />
<EditText
android:id="@+id/upName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/upEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:id="@+id/upPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Phone" />
<EditText
android:id="@+id/upDesignation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Designation" />
<Button
android:id="@+id/updateEmploy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update Employee" />
</LinearLayout></ScrollView></LinearLayout>


For better understanding the sample code is given above.

Any help would be highly appreciated

Answer

The problem is that you use wrong View IDs. In your XML there is upName, and you are searching for Resource.Id.eName.
To fix a problem you need to change Resource.Id.eName to Resource.Id.upName

Comments