0

I am new to mobile development. I am working on android using xamarin in visual studio 2015. I am following this sample code. I am deploying my app on my device and it's running good. I have basically following functions

  1. Insert
  2. Display
  3. Update
  4. Delete

All of the functions are running good but on update when i click the update button in update layout first it gets all the data from DB(SQLITE) and then it displays it in Edit Text Boxes In sample code you can see the updateEmployee.cs code. So while updating i m getting the bellow error

enter image description here

I am getting null value in dName while there is a name in name field

Above i have declared the dName dEmail etc like bellow

TextView dName, dEmail, dPhone, dDesignation;
    String name, email, phone, designation;

Also in initialize method i am also getting null values and eName etc in initialize is the id in complete_data layout whose axml is as follows

<?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 1:

Bellow is the code in which i am getting this exception

 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;
    }

The name field is in string and i am assigning this string to my edit text dName and etc as shown in code above. At dName.text = name i am getting this null exception, while the name field contain the name. Is there any other way to do it?

I am not sure what am i missing. Any help would be highly appreciated

7
  • So why do you have TextViews in axml but EditTexts in code, please use the same time for starter Commented Nov 8, 2016 at 7:21
  • @OlegBogdanov data in update layout is coming from complete_data layout and in this layout only the data view is available i.e. you can only see the data, while in update layout edit text is use so the selected record (coming from complete_data) should be placed in edit text and i can be able to edit it. That's why i have used edit texts in code Commented Nov 8, 2016 at 7:36
  • Well, either you have pasted wrong portion of the code or your code is wrong. In your XML file you are showing TextView elements and in your initialization method you are trying to cast ename to EditText, that's not supposed to work. Please show where you call initialize and what exact axml it relies on Commented Nov 8, 2016 at 7:42
  • @OlegBogdanov kindly see the Update 1 Commented Nov 8, 2016 at 7:55
  • 3
    Possible duplicate of What is a NullReferenceException, and how do I fix it? Commented Nov 8, 2016 at 8:20

3 Answers 3

1

The most probable cause for this problem is that FindViewById is either not called or it doesn't actually assign dname.

This can have multiple causes:

  1. Your initialize method is never called
  2. It is called after List_ItemClick
  3. It is called but assigning the view fails because in XML use have TextView and in code the type is EditText

Set two breakpoints. One in List_ItemClick and one in initialize. Check which one is called first. Step through initialize to make sure views are found and assigned.

Sign up to request clarification or add additional context in comments.

4 Comments

initialize is called first but it's not assigning any value and it's obvious because on first run the text boxes(edit texts) are empty in update layout. I have to select a record after selecting the record the data in it should be shown and then i should be able to edit the data please see this link androidcodec.com/wp-content/uploads/2016/08/…
Maybe I'm misunderstanding what you want, but the question whether or not there is text in an edit text is completely irrelevant to the question why it won't be found in FindViewById.
Kindly see this link i am following it as a sample code androidcodec.com/xamarin-sqlite-database-example-android this will help you understand more correctly
You don't stick to the example you've linked. While in your XML the fields are TextView and in your code the variables are declared TextView, your initialize method casts to EditText. That won't work! If you use sample code, please make sure to copy & paste correctly.
0

First of all, I can see from your code that you declare dName as a TextView but when you use FindViewById your are parsing EditText instead, so make sure that you use TextView in both places.

Second, make sure that your code runs in the following sequence: 1- Declare dName as a TextView. 1- Assign a value to "name" variable. 2- Use FindViewById to find eName. 3- Assign dName a value.

so, your code should look like the following order:

TextView dName;
string name = "Some name"; // or you can get the name from database
dName = (TextView)FindViewById(Resource.Id.eName);
dName.Text = name;

Comments

0

This is a usual Problem While building again am again , here are the steps I get resolve:

  1. If you xamarin Auto Update is on then your check your project setting and target Android version ,, If it is updated then (a) downgrade it or (b) Download the new versions SDK by going to Tools--> SDK manager
  2. Clean up your simulator with all instances of APP
  3. Clean Build
  4. Shudown Computer..then Start system ( dont restart it )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.