0

this is the line of code where it shows the error

var physician = (from userAccounts in myDb.Physicians
                            where userAccounts.Phy_UserName == txtUserName.Text
                            select userAccounts).FirstOrDefault();

            setFirstName(physician.Phy_FName);

But in my setter I have a pre condition that if it's a null value it wouldn't do anything but how come it still shoes that error? here's my setters code

public void setFirstName(string fName) {
            if (fName == null)
            {
            }
            else {
                firstName = fName;
            }
        }
user962206
  • 15,637
  • 61
  • 177
  • 270
  • 1
    possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Mar 02 '12 at 04:40

5 Answers5

4

I'm guessing that your physician query returns null.

You're then attempting to call a property, Phy_FName, on a null value.

Checking for a null value in your setFirstName method will not protect you in this case, because Phy_FName isn't what's null, physician is.

As an aside, you may also want to check that fName isn't an empty string in your setFirstName method. You could check against both conditions by using if (!String.IsNullOrEmpty(fName))

Jeremy Wiggins
  • 7,239
  • 6
  • 41
  • 56
  • That was my assumption at first, however the FirstOrDefault should prevent against that. – Justin Pihony Mar 02 '12 at 04:30
  • 5
    no, FirstOrDefault() will return a null value if there's no results. First() will throw an exception. – Eric Mar 02 '12 at 04:31
  • Sorry, I wasnt thinking of what the default value of a reference type was. My apologies, and thanks for reminding me :) – Justin Pihony Mar 02 '12 at 04:35
  • I would also point out that if the username is unique in the table, you should use .Single() NOT .First() (or .SingleOrDefault) – Quango Mar 02 '12 at 04:37
  • As you can see on my code. I am also expecting a null value. though on my setter there's a pre condition where if the fname has null value don't do anything. – user962206 Mar 02 '12 at 04:39
  • 2
    @user962206 - See my update answer. The problem isn't that `Phy_FName` is null, it's that `physician` is null. If `physician` is null, then attempting to call any property or method that belongs to it will result in the exception you're seeing. – Jeremy Wiggins Mar 02 '12 at 04:42
  • @JeremyWiggins I understand, thank you. soo basically what it does is Physician object itself is null therefore other rows in it is null, and with that I am getting this error? – user962206 Mar 02 '12 at 04:52
  • 1
    @user962206 - Sort of. Imagine I ask you to read a page out of a book. But I never gave you the book. ... You're trying to access something on a `physician`, but you have no `physician`. I would recommend reading the link that @John Saunders posted in the comment to your question. I think it would be very helpful for you. – Jeremy Wiggins Mar 02 '12 at 04:59
1

Try this:

var physician = (from userAccounts in myDb.Physicians
                            where userAccounts.Phy_UserName == txtUserName.Text
                            select userAccounts).FirstOrDefault();

if(physician != null)
{
  setFirstName(physician.Phy_FName);
}
else
{
  //Throw Error or any any other processing as needed.
}
Chandu
  • 81,493
  • 19
  • 133
  • 134
0

I bet physician is null. Are you sure you're getting anything back from the query?

Eric
  • 3,284
  • 1
  • 28
  • 29
0

The Phy_FName property will be evaluated either way so make sure physician isn't null before trying to read its properties.

You'll need to check the value of physician before attempting to use it:

if (physician != null)
    setFirstName(physician.Phy_FName);
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
0

To clarify on a wrong assumption of my own that maybe you were thinking also:

FirstOrDefault will return defaults if there is one, but the default for an object is null. So, most likely your Linq query is returning a null value for physician. Of which you are then trying to access the Phy_FName.

Here is a SO question that clarifies this more in depth: Specify default value for a reference type

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180