0

This might be a silly qeustion.

I am not sure why I am getting the below warning on txtMiddleName.Text.Trim() but not on the other textBox controls.

// Set Full Name Text
txtFullName.Text = txtFirstName.Text.Trim() + " " + 
                   txtMiddleName.Text.Trim() + " " + 
                   txtLastName.Text.Trim();

Warning CS8602 Dereference of a possibly null reference.

enter image description here

Updated

The textbox(s) were declared automatically when designing WinForms

this.txtLastName = new System.Windows.Forms.TextBox();
this.label13 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.txtMiddleName = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.txtFirstName = new System.Windows.Forms.TextBox();
Unis
  • 365
  • 1
  • 4
  • 13
  • 3
    You would have to show how txtMiddleName is declared. – David L Aug 26 '22 at 00:41
  • 1
    please refer to this post https://stackoverflow.com/questions/59306751/why-does-this-code-give-a-possible-null-reference-return-compiler-warning – jmvcollaborator Aug 26 '22 at 01:06
  • I have updated the q to include the declaration – Unis Aug 26 '22 at 07:31
  • 1
    @Unis - That's where it is instantiated, not where it is declared. – Enigmativity Aug 26 '22 at 07:34
  • 1
    There'll be an earlier piece of your code that does something else with `txtFirstName.Text` and `txtLastName.Text` such that it's already established at this point that they cannot be null. – Damien_The_Unbeliever Aug 26 '22 at 07:45
  • Thank you all, your comments and hint did help. So the `txtMiddleName .Text` actually gets assigned to Entity Property eariler in the code which is actually nullable. – Unis Aug 26 '22 at 13:20

0 Answers0