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

  • So why do you have TextViews in axml but EditTexts in code, please use the same time for starter – Oleg Bogdanov Nov 08 '16 at 07: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 –  Nov 08 '16 at 07: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 – Oleg Bogdanov Nov 08 '16 at 07:42
  • @OlegBogdanov kindly see the **Update 1** –  Nov 08 '16 at 07:55
  • well you still have TextView and EditText mix, can you change either XML or code to use the same type? – Oleg Bogdanov Nov 08 '16 at 07:58
  • 3
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Kirill Nov 08 '16 at 08:20
  • @g4s8 I've had this discussion before. I'm pretty sure that the OP knows what a NRE is. The question is, why does he get one in his special case even though he tries to make sure not to get one. – Thorsten Dittmar Nov 08 '16 at 08:46

3 Answers3

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.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • `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 http://www.androidcodec.com/wp-content/uploads/2016/08/Screenshot_2016-08-15-01-44-24-horz.png –  Nov 08 '16 at 09:16
  • 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`. – Thorsten Dittmar Nov 08 '16 at 09:23
  • Kindly see this link i am following it as a sample code http://www.androidcodec.com/xamarin-sqlite-database-example-android/ this will help you understand more correctly –  Nov 08 '16 at 09:27
  • 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. – Thorsten Dittmar Nov 08 '16 at 09:38
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;
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 )