none
Linq object reference not set RRS feed

  • Question

  • Hello , I just started with xamarin and c# and i struggle to understand why i'm getting this error here's my code it's supposed to be searching in list using linq and displaying the selected element in the list based on "Fixtureid"  from a guide i found on youtube , the video its a bit old but i tought it might still be good .

    namespace FixtAppl
    {
        [Activity(Label = "Fixture")]
        public class Fixture : Activity
        {
            private List<Fixture_List> mItems;
            public ListView fixList;
            public EditText fixSearch;
            private CustomAdapter fixtAdapter;

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


                //Set View
                SetContentView(Resource.Layout.Fixture);
                //Set View
                //ViewConnect
                fixList = FindViewById<ListView>(Resource.Id.FixtureList);
                EditText fixSearch = FindViewById<EditText>(Resource.Id.FixSearch);
                fixSearch.Alpha = 0;
                fixSearch.TextChanged += FixSearch_TextChanged;
                //ViewConnect

                // List
                mItems = new List<Fixture_List>
                {
                    new Fixture_List { FixtureId = "9771", FixtureName = "VNC 8 - LED ARRAY LH", FixtureRaft = "01", FixtureNumar = "01" },
                    new Fixture_List { FixtureId = "9772", FixtureName = "VNC 8 - LED ARRAY RH", FixtureRaft = "01", FixtureNumar = "02" },
                    new Fixture_List { FixtureId = "9773", FixtureName = "VNC 8 - Led DRIVE MODULE", FixtureRaft = "01", FixtureNumar = "03" },
                    new Fixture_List { FixtureId = "1877", FixtureName = "VNC 1 - INNER TAIL", FixtureRaft = "01", FixtureNumar = "04" },
                    new Fixture_List { FixtureId = "1878", FixtureName = "VNC 1 - INNER TAIL", FixtureRaft = "01", FixtureNumar = "04" },
                    new Fixture_List { FixtureId = "6181", FixtureName = "VNC 1 - LDM", FixtureRaft = "01", FixtureNumar = "05" },
                    new Fixture_List { FixtureId = "6184", FixtureName = "VNC 1 - LDM", FixtureRaft = "01", FixtureNumar = "05" },

    }

               fixtAdapter = new CustomAdapter(this, mItems);
                fixList.Adapter = fixtAdapter;


            }

            private void FixSearch_TextChanged(object sender, TextChangedEventArgs e)
            {
                List<Fixture_List> searchFixture = (from fixt in mItems where fixt.FixtureId.Contains(fixSearch.Text) select fixt).ToList();
                fixtAdapter = new CustomAdapter(this, searchFixture);
                fixList.Adapter = fixtAdapter;
            }
        }
    }

    The error i'm getting is this 

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

    Here's a photo of the error i'm getting maybe it helps

    https://imgur.com/E9xbux2

    The line's of code that i'm getting the error in are those 

      private void FixSearch_TextChanged(object sender, TextChangedEventArgs e)
            {
                List<Fixture_List> searchFixture = (from fixt in mItems where fixt.FixtureId.Contains(fixSearch.Text) select fixt).ToList();
                fixtAdapter = new CustomAdapter(this, searchFixture);
                fixList.Adapter = fixtAdapter;
            }

    Wednesday, May 8, 2019 8:37 AM

Answers

  • Hi i think i found where my querry is null, the fixSearch.Text shows as null when i hover over it . Do i have to add a if / else to tell it what to do in case fixSearch.Text is null ?

    If you are using a modern version of C#, one easy fix is to write it like this:

    fixt.FixtureId.Contains(fixSearch?.Text ?? string.Empty)

    The first ? means "if ficSearch is null, return null without accessing .Text". The second ?? means "return what we wrote at right if the left part is null".

    • Marked as answer by Andrei Ciopas Friday, May 10, 2019 11:05 AM
    Wednesday, May 8, 2019 12:31 PM
    Moderator

All replies

  • The error that you are seeing means that something in that line is null and you are trying to read something from within that null object.

    (from fixt in mItems where fixt.FixtureId.Contains(fixSearch.Text) select fixt).ToList();

    One first possibility is that "mItems" is null. This would happen if FixSearch_TextChanged is triggered before OnCreate is executed. This is unlikely but you shuld still verify it using the debugger.

    A second possibility is that one of the items in mItems contains a null FixtureId. This should not happen, given the way in which you initialise mItems. But you should still verify it with the debugger in case the list s modified elsewhere in your code.

    The final one is that fixSearch is null, so fixSearch.Text would error-out.

    In summary, you need to identify what exactly is null in your query. When Visual Studio stops at the error, examine each of the variables until you find out which one is null, and then work out at finding how it came to be null at the time when you executed the error statement.

    Wednesday, May 8, 2019 9:14 AM
    Moderator
  • Hi i think i found where my querry is null, the fixSearch.Text shows as null when i hover over it . Do i have to add a if / else to tell it what to do in case fixSearch.Text is null ?

    List<Fixture_List> searchFixture = (from fixt in mItems where fixt.FixtureId.Contains(fixSearch.Text) select fixt).ToList();
                fixtAdapter = new CustomAdapter(this, searchFixture);
                fixList.Adapter = fixtAdapter;

    The bolded fixSearch.Text is returning as null

    • Edited by Andrei Ciopas Wednesday, May 8, 2019 9:34 AM beeing more specific
    Wednesday, May 8, 2019 9:27 AM
  • You put some validation logic  for fixSearch.Text and display a message that data is required if it is null   If the object is null don't do the search, or you fix  fixSearch.Text to not be null and have an empty string so the search will not fail.

    But why is  fixSearch.Text null? That's what you need to debug and fix.

    Wednesday, May 8, 2019 11:31 AM
  • Try the following with a breakpoint on the first line of code below.

    if (fixSearch != null) 
    {
        if (!string.IsNullOrWhiteSpace(fixSearch.Text))
        {
            // run your code
        }
        else
        {
            // no contents in fixSearch.Text
        }
    }
    else
    {
        // fixSearch has not be created or was destroyed
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 8, 2019 12:11 PM
    Moderator
  • Hi i think i found where my querry is null, the fixSearch.Text shows as null when i hover over it . Do i have to add a if / else to tell it what to do in case fixSearch.Text is null ?

    If you are using a modern version of C#, one easy fix is to write it like this:

    fixt.FixtureId.Contains(fixSearch?.Text ?? string.Empty)

    The first ? means "if ficSearch is null, return null without accessing .Text". The second ?? means "return what we wrote at right if the left part is null".

    • Marked as answer by Andrei Ciopas Friday, May 10, 2019 11:05 AM
    Wednesday, May 8, 2019 12:31 PM
    Moderator