-2

I'm using WPF .net 5.0 framework. I'm trying to display a value that I passed into a textbox for users to edit the current value.

This is the code

    public EditUserDetailsPage1(User user)
    {
        InputFullName.Text = user.Full_Name;
        InputFirstName.Text = user.First_Name;
        InputLastName.Text = user.Last_Name;
        InputEmail.Text = user.Email;
        inputUsername.Text = user.Username;
        InitializeComponent();
    }

This is the XAML

<TextBox Style="{StaticResource TextBoxExtend}" hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="Full Name " VerticalAlignment="Top" hc:InfoElement.Placeholder="Enter full name" Margin="24,75,564,0" Name="InputFullName"/>
<TextBox Style="{StaticResource TextBoxExtend}" hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="First Name  " VerticalAlignment="Top" hc:InfoElement.Placeholder="Enter First Name" Margin="439,75,296,0" Name="InputFirstName"/>
<TextBox Style="{StaticResource TextBoxExtend}" hc:TitleElement.TitlePlacement="Left" hc:TitleElement.Title="Last Name   " VerticalAlignment="Top" hc:InfoElement.Placeholder="Enter last Name" Margin="709,75,26,0" Name="InputLastName"/>
<TextBox Style="{StaticResource TextBoxExtend}" hc:TitleElement.TitlePlacement="Left" hc:TitleElement.Title="Email" VerticalAlignment="Top" hc:InfoElement.Placeholder="UserEmail@gmail.com" Margin="24,142,564,0" Name="InputEmail"/>
<TextBox Style="{StaticResource TextBoxExtend}" hc:TitleElement.TitlePlacement="Left" hc:TitleElement.Title="Username" VerticalAlignment="Top" hc:InfoElement.Placeholder="Enter username" Margin="439,142,253,0" x:Name="inputUsername"/>

I've been using this method in other pages and it works perfectly fine. But for this page, it gives me this error

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

I tried other methods like declaring it first

    User passeduser;
    public EditUserDetailsPage1(User user)
    {

        passeduser = user;

        InputFullName.Text = passeduser.Full_Name;
        InputFirstName.Text = passeduser.First_Name;
        InputLastName.Text = passeduser.Last_Name;
        InputEmail.Text = passeduser.Email;
        inputUsername.Text = passeduser.Username;
        InitializeComponent();
    }

As you can see here, there's a value being retrieved enter image description here

but it still giving me an error. I followed the same method i used for my other pages and it just doesn't seem to work for this page only. Greatly appreciate any help!

ElaVnla
  • 27
  • 7
  • 1
    the accepted answer is correct, but just for generic null exception advice - if you want it to work even if user was null you can use expressions like ' user?.Full_Name ?? "No User" '. The ?. expression returns null if the left hand side is null instead of throwing an exception. The ?? returns the right hand value if the left side is null. It's also advisable to put a try{}catch{} exception around methods that might fail like this, so if a user has the issue they see "Provided user was invalid" instead of "Object not set to an instance" – The Lemon May 09 '22 at 04:37

1 Answers1

2

It's because you're trying to use InputFullName before it's been created.

In WPF, the UI elements are created when you run InitializeComponent.

Try this instead:

public EditUserDetailsPage1(User user)
{
    InitializeComponent();

    InputFullName.Text = user.Full_Name;
    InputFirstName.Text = user.First_Name;
    InputLastName.Text = user.Last_Name;
    InputEmail.Text = user.Email;
    inputUsername.Text = user.Username;
        
}
Loocid
  • 6,112
  • 1
  • 24
  • 42
  • 1
    Just to emphasise the point, calling `InitializeComponent` should ALWAYS be the first thing you do in a WPF window or WinForms form constructor. The default constructor will already contain the call and you should put your own initialisation code after it. If you add a constructor with parameters but retain the parameterless constructor, leave the call in the parameterless constructor and have the new constructor invoke it. – John May 09 '22 at 03:11