Hello! When clicking an text-entry-box i get null reference exception object reference not set to an

Jane_NatalieJane_Natalie Member ✭✭✭

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

This is my xaml code from the page in which the text-entry-box is in. The exception is thrown when I click on the entry box, before I am able to type anything in it.
----xaml, the page with the entry box--------

<ContentPage.BindingContext>

</ContentPage.BindingContext>

<ContentPage.Content>
    <StackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="10">

        <Entry Text="{Binding Keyword}"  MaxLength="15"/>

        <Button Text="Send" BorderColor="Black" TextColor="White"   FontAttributes="Bold" FontSize="20" BackgroundColor="#f49131" 
    Command="{Binding CheckCommand}" x:Name="send" IsVisible="true" CornerRadius="10"/>

</StackLayout>

</ContentPage.Content>

And this is the code from the viewmodel, which the bindingcontext to the former page ^^

public string Keyword { get; set; }  <----- this is what the entry box's text is binded to.
    public int ID { get; set; }

    public ICommand CheckCommand
    {
        get
        {
            return new Command(async () =>
            {


                Checks = await _apiServices.GetCheckAsync(Keyword);
                if (_checks.Count > 0)
                {                         
                        Settings.Chk = "true";                          

                        await Application.Current.MainPage.DisplayAlert("message", "message.", "ok");                        
                 } 
                    else
                    {
                        await Application.Current.MainPage.DisplayAlert("Feil kode", "Det oppstod en feil.", "ok");
                    }
                }        
            });
        }

}

Best Answer

  • Jane_NatalieJane_Natalie ✭✭✭
    Accepted Answer

    The solution was to null check a list. The source of the problem was in my viewmodel code and not the xaml page.
    if(mylist.Count == 0 && mylist == null) seems to work. @rgannerlla @AnnR

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Your code looks no problem ,could you share us a basic sample for the reproduction?

  • AnnRAnnR Member ✭✭

    Hi

    I'm getting the same problem since I downloaded the update to Xamarin.Forms - 4.0.0.425677. An application that had been working is now giving me
    "Unhandled Exception:

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

    on every page that has an Entry Box

    One page I go into that has no data in the Entry Box - the focus is placed on the Entry Box and the app closes immediately. I've debugged and it is stepping through the code in the constructor and then it just closes with the error. It is not going through any of my other code.

    I've uninstalled this update and went back to version - 4.0.0.394984-pre10 and my app now works.

    The one reason I had updated was to check if a problem I have where I have a stack or grid with an entry box invisible when the page appears. And then something happens on the page and I make the stack or grid visible - when I click into the Entry box - you can only see the bottom half of the text you are typing - its like the control isn't sizing properly. But if I have the stack/grid visible when the page appears, my entry box is ok.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @AnnR said:
    Hi

    I'm getting the same problem since I downloaded the update to Xamarin.Forms - 4.0.0.425677. An application that had been working is now giving me
    "Unhandled Exception:

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

    on every page that has an Entry Box

    One page I go into that has no data in the Entry Box - the focus is placed on the Entry Box and the app closes immediately. I've debugged and it is stepping through the code in the constructor and then it just closes with the error. It is not going through any of my other code.

    I've uninstalled this update and went back to version - 4.0.0.394984-pre10 and my app now works.

    The one reason I had updated was to check if a problem I have where I have a stack or grid with an entry box invisible when the page appears. And then something happens on the page and I make the stack or grid visible - when I click into the Entry box - you can only see the bottom half of the text you are typing - its like the control isn't sizing properly. But if I have the stack/grid visible when the page appears, my entry box is ok.

    @AnnR so it's a bug in the xamarin.forms 4.0.0425677 version then? Anyways, the weird thing is that this exception is only thrown in my one application, in other applications where I use the exact same code, the error isn't thrown. I guess I just have to go back to the earlier version for now untill it's fixed.

    @coleX How can I make a sample? If I create a new cross platform project, this exact code works. The exception is only thrown in my rather large application.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited May 29

    Revert to earlier version of Forms to see if problem persists.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @ColeX when using an earlier xamarin forms version everything works just fine. Has to be something with the new version

  • ColeXColeX Member, Xamarin Team Xamurai

    Yeah , you could file issue on XF github :https://github.com/xamarin/Xamarin.Forms/issues

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @ColeX Ok, so the exception has now, one day later, started throwing again. I have no clue what is doing it, and there is no way to put it in a try catch to see the error message, as it is only thrown when I click a text-entry box. Do you have any idea what the problem could be? Or any tips at all for that matter. I really appreciate the help.

  • ColeXColeX Member, Xamarin Team Xamurai

    Sorry without reproduction sample i can't figure out the cause , since it works perfectly on my side.

  • rgannerllargannerlla Member ✭✭

    @ThankYou I have similar issue. Did you figure out the fix?

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @rgannerlla it seems like a lot of people are experiencing the same issue. And no I did not find a solution yet.

  • rgannerllargannerlla Member ✭✭

    @ThankYou @ColeX
    When I downgrade to 4.0.0.394984-pre10 until 3.0, the app still crashes with the exception 'Java.lang.illegalexception'.
    I can click and enter text in text-entry-box but clicking 'Done' button on keyboard crashes the app. This happens for all Text Entry's in the app and only in Android. Please let me know if you are facing similar issues in previous versions as well.

  • rgannerllargannerlla Member ✭✭

    @ColeX Seems like the issue I had with app crash has been resolved in this commit. github.com/adrianknight89/Xamarin.Forms/commit/b8bed2d9ab031154c299e5c7e0451a2ff522ce9d
    Can you please tell me when this will be released?

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @rgannerlla when I downgraded to earlier xamarin forms versions, the first time i tried deubgging again, it actually worked, and after that I get the same null reference exception on all versions.

  • Jane_NatalieJane_Natalie Member ✭✭✭
    Accepted Answer

    The solution was to null check a list. The source of the problem was in my viewmodel code and not the xaml page.
    if(mylist.Count == 0 && mylist == null) seems to work. @rgannerlla @AnnR

  • JohnHJohnH GBMember ✭✭✭✭✭

    @ThankYou said:
    The solution was to null check a list. The source of the problem was in my viewmodel code and not the xaml page.
    if(mylist.Count == 0 && mylist == null) seems to work. @rgannerlla @AnnR

    I am guessing you mistyped the above, if myList were null the myList.Count would throw a null ref exception. If you need both checks they should be the other way around.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JohnH Yeah, it's != not ==.

  • JohnHJohnH GBMember ✭✭✭✭✭

    @ThankYou said:
    @JohnH Yeah, it's != not ==.

    Ah no it should be:
    if(mylist != null && mylist.Count == 0)

    If you check count first, like this:
    if(mylist.Count == 0 && mylist != null)
    If myList is null, then the above code will throw an exception.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JohnH I meant if(mylist != null && mylist.Count != 0) :D

  • JohnHJohnH GBMember ✭✭✭✭✭

    @ThankYou said:
    @JohnH I meant if(mylist != null && mylist.Count != 0) :D

    Perfecto!

  • AnnRAnnR Member ✭✭

    @ThankYou said:
    @JohnH I meant if(mylist != null && mylist.Count != 0) :D

    Only getting back to my project now - will try this. Thanks for posting.

Sign In or Register to comment.